I want to do this:
While the main Thread runs,
I want to execute a series of actions without disturbing the main thread and freezing the swing components and I can force stop the execution from the main Thread. And one of this actions can manipulate a swing component.
Some code to help imagining:
public class Test extends JFrame{
public Test()
{
super("Test");
JButton button = new JButton("Stop!");
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
//STOP THE EXECUTION
}
});
this.add(button);
//START OF EXECUTION
System.out.println("FOO");
System.out.println("BAR");
button.setText("Hello");
System.out.println("THIS");
//END OF THE EXECUTION
this.pack();
this.setVisible(true);
}
}
I’ve tried to use only Thread, it throws exception, I forgot what the exception was, but basically it says that I can’t manipulate the swing component from non AWT thread or something.
After that I tried using both Thread and swingUtilities, but they execute the swing component manipulation AFTER the execution done. How do I do this?
see Oracle tutorial about Cuncurency in Swing
Swing Workers methods publish(), process() and done() quite guaranted tha all output to the Swing GUI will be done on EDT
SwingWokreris implementetion forWorkersThread and Swing, and during its execution are allSwing JComponents accesible forMouseandKey Events