K
K
krembrule20162018-11-05 14:31:14
Java
krembrule2016, 2018-11-05 14:31:14

How to make a button move on click?

Good afternoon!

nButton1.setOnAction(new EventHandler<ActionEvent>(){
  public void handle(ActionEvent ae){
    for(int i=0; i<30; i++){
      translate.setX(0); 
        translate.setY(i); 
        translate.setZ(0);
    nButton1.getTransforms().add(translate); 
    try {
                th.sleep(33);
            } catch (InterruptedException ex) {
                Logger.getLogger(Big_shot.class.getName()).log(Level.SEVERE, null, ex);
            }
    }	
    }
  });

I wrote this piece of code. According to my logic, when we click on the button, we should get into a cycle in which the button will go a little north.
However, this does not happen. And the button just disappears. Where am I wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-11-05
@krembrule2016

When a window is created , an Event Processing Thread is created , inside which the event loop and event handlers will work. Any events are sent to the event queue, from where they are retrieved and processed at the next iteration of the event loop. Therefore, it is useless to use loops to change the state of GUI elements. Also, you should not try to interact with GUI elements from the main thread or any other thread - this will cause a crash. You should not start threads inside event handlers - this will lead to a crash. And you shouldn't stop the thread - that will crash.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question