JButton ActionListener - GUI update only after clicking JButton

I have a problem with my JButton ActionListener. I have a doTheCleaning () method defined in another class that, when called, makes a number of changes to my GUI.

public void doTheCleaning(){ //change image icon //had thread.sleep here //insert to text area //had thread.sleep here //etc } 

Then in another class I created an instance of a class containing my doTheCleaning () method, and my ActionListener was written with my actionperformed () method for my jbutton, written as follows:

 public void actionPerformed(ActionEvent e){ //some code //newClass.doTheCleaning(); } 

I know how to do the rest, like addActionListener () and stuff, so no need to ask about it. My concern is that all the changes in my GUI that are made when the toTheCleaning () method is called are applied only after the button is clicked. When this happens, the sequence of changes that occurred in my shortcuts and text box was not displayed. The code works fine if I called it directly in the tester class, but calling it inside the actionperformed method shows only the final state of my GUI. I need to show which item changed first, then next, etc.

How can I achieve this when I need these changes only when I click JButton?

** I am not very good at GUI in java. I hope you guys understood my point without my code. but I could if necessary. Thank you

+5
source share
1 answer

Do not perform intensive operations in the EDT , otherwise the GUI will be out of date and you may not see GUI updates. The best choice you can use is SwingWorker :

For more on this, read Concurrency in Swing .

+12
source

Source: https://habr.com/ru/post/921560/


All Articles