#while and action listener
53 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @prisma flint! Please use
/closeor theClose Postbutton 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.
I suggest you post the code in text form and in a codeblock.
should i post all of the code ?
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.
you don't update buttonPressed in your while loop
yeah becuase i want it to update only after presssing button
yeah but you have to update the state of buttonPressed within the loop otherwise you will never exit the loop
how can i do that bro
Damn, I just read that the MouseListener doesn't have to be integrated. Sorry for confusing you mate, Java GUI's are weird 😄
no probs bro , java sucks , but love it to death , so have to do this
Does your code print the currentTimeMillis() permanently?
nah nah , its just to know whats happening
yes bro , that statement does work
the whole code blockwithin the while loop works
but it doesnt stop
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?
i understood like 2% of what you wrote , idk what is atomic , volatile or anythgin
did the thraed even reach the buttonPressed = true statement ?
or does the thread get stuck in the while loop
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?
yoo that kinda works me gonna work on that
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;
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
damn , thanks a lot bro , this prolly will make it qork
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.
dude all this shit happening just to run somw shitty code lmaoo
yeah it can suck quite a bit
@cunning sinew you have any good resources to leanr abt java compilers and how it runs code
yes
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
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.
hmm its fine
cheers