The reason because it is not working right now is that the Java thread that handle the GUI refreshing also handles the events of the Listeners. So when you call the setText()
method it tells the GUI thread (called EDT for Event Dispatch Thread) to update the Component, but it can't be done right now because the EDT is currently in the actionPerformed()
method executing your code.
So I think you should put the code that do whatever work and change the text of your JLabel in a new thread. So the EDT starts it in the actionPerformed()
and is then free to update the GUI when the text of your JLabel changes.
Something like this: (You have to implement the run method)
public void actionPerformed(ActionEvent e) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
myLabel.setText("Processing");
//Do the job
myLabel.setText("Processed");
}
});
t.start();
}
Ideally the setText()
method and others that alter the component have to be called from the EDT itself to avoid bugs... This is not the case in the example I gave you. If you want to do it use this method:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
myLabel.setText("my text");
}
});