#while and action listener

53 messages · Page 1 of 1 (latest)

prisma flint
#

I need to stop the while loop if i click on the panel , but the code doesnt even reach the actionlistener
Can somebody help

cunning latchBOT
#

This post has been reserved for your question.

Hey @prisma flint! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

kindred slate
prisma flint
#

should i post all of the code ?

pearl eagleBOT
#

counter = System.currentTimeMillis() ; ```java

 while(buttonPressed == false) {
    
     if(System.currentTimeMillis() - counter > 500 && a == 0 ) {
         label.setText("PRESS TO START");
         counter = System.currentTimeMillis() ; 
         a = 1 ; 
         System.out.println(System.currentTimeMillis()) ; 

     }
     if(System.currentTimeMillis() - counter > 500 && a == 1) {
         label.setText(null);
         counter = System.currentTimeMillis() ; 
         a = 0 ; 
     }
 } ```
 
 panel.addMouseListener(new MouseListener() {

           @Override
        public void mouseClicked(MouseEvent e) {
               buttonPressed = true ;

This message has been formatted automatically. You can disable this using /preferences.

kindred slate
#

you don't update buttonPressed in your while loop

prisma flint
#

yeah becuase i want it to update only after presssing button

kindred slate
#

yeah but you have to update the state of buttonPressed within the loop otherwise you will never exit the loop

prisma flint
#

how can i do that bro

kindred slate
#

Damn, I just read that the MouseListener doesn't have to be integrated. Sorry for confusing you mate, Java GUI's are weird 😄

prisma flint
#

no probs bro , java sucks , but love it to death , so have to do this

kindred slate
#

Does your code print the currentTimeMillis() permanently?

prisma flint
#

nah nah , its just to know whats happening

kindred slate
#

Yeah, does it print?

#

I want to know what is happening too 😄

prisma flint
#

yes bro , that statement does work

#

the whole code blockwithin the while loop works

#

but it doesnt stop

cunning sinew
#

I suggest you make the buttonPressed an atomic variable

#

and put some kind of sleep in the while loop

#

it's likely that the thread is caching the buttonPressed value and never seeing that it got updated by the UI thread

#

or if not an atomic variable at the very least volatile

#

well actually I can't really see whether it's volatile. Is it?

prisma flint
#

i understood like 2% of what you wrote , idk what is atomic , volatile or anythgin

prisma flint
#

or does the thread get stuck in the while loop

cunning sinew
#

well without seeing the full context I can't tell exactly what is happening

#

from what I can see in the given code it's possible you never actually add the mouse listener to the panel because the statement to add it is after the while loop

#

why don't you move panel.addMouseListener above the while loop to begin with?

prisma flint
#

yoo that kinda works me gonna work on that

cunning sinew
#

still even if you do that you should make the variable atomic/volatile. Making a variable volatile is as simple as writing "volatile" before the type of the variable (only works for global variables) such as this:

 private volatile boolean foo=false;
cunning sinew
# prisma flint what does this even do

Events are fired on a thread separate from your main thread (either the UI thread or a thread specifically made for firing UI events, can't remember what was the case with javafx or swing or whatever. In any case the thread is different)

#

now when a thread is executing code it might choose to cache some variable to optimize

#

in other words if the variable buttonPressed has been false the last 20 times it was checked the compiler might decide to just make it false and not even check it

#

which is bad if it gets changed in another thread

#

the keyword "volatile" just tells the threads to not do such caching

#

and an atomic variable is just a volatile variable with the added benefit of two threads not being able to access the same variable at the same time. That's called atomicity. If they can access at the same time but no caching is guaranteed that's volatility

#

in general you should in most cases just make it atomic but in your case volatile will do the job

prisma flint
#

damn , thanks a lot bro , this prolly will make it qork

cunning latchBOT
prisma flint
#

dude all this shit happening just to run somw shitty code lmaoo

cunning sinew
#

yeah it can suck quite a bit

prisma flint
#

@cunning sinew you have any good resources to leanr abt java compilers and how it runs code

prisma flint
cunning sinew
#

well I suppose if you want to know how things work in depth the java spec is probably the place to look

#

I can't say for sure though I've rarely read it to make sure of some stuff

prisma flint
#

thanks a lot mate

cunning latchBOT
# prisma flint thanks a lot mate

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.