Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing: Can't get JButton to update - repaint() not working

I'm using Swing for the first time to create a simple GUI. It consists of a JFrame upon which I have placed a single JButton which, when clicked, calls some other code which takes approx. 3 seconds to return.

Just before the call to this code, in actionPerformed(), I want to update the text on the button to inform the user that processing is occuring. My problem is that the text on the button does not update until after the 3-second call has returned. I want the updated text to be present during the call, then I'll change it back afterwards.

Calling repaint() on the JButton doesn't do anything and calling it on the JFrame results in "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" being thrown when I click the button.

like image 939
William Avatar asked Jun 10 '09 19:06

William


2 Answers

What's happening is that the 3-second code is executing in the GUI thread, so the button doesn't have a chance to update until it's done.

To solve this, start a SwingWorker to do the long-running operation; then you'll still be free to do things in the GUI while you're waiting for it.

Here are a couple of tutorials on the subject, and the SwingWorker Javadocs referenced above have some code also.

Sample code

public void actionPerformed(ActionEvent e) {
    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
        @Override
        public Void doInBackground() {
            // Call complicated code here
            return null;
            // If you want to return something other than null, change
            // the generic type to something other than Void.
            // This method's return value will be available via get() once the
            // operation has completed.
        }

        @Override
        protected void done() {
            // get() would be available here if you want to use it
            myButton.setText("Done working");
        }
    };
    myButton.setText("Working...");
    worker.execute();
}
like image 191
Michael Myers Avatar answered Nov 02 '22 14:11

Michael Myers


The problem here is that your long running task is blocking the thread that would normally paint the GUI.

The usual way around this is to throw the longer running task off into another thread.

This can be done fairly easily using a SwingWorker.

This question may provide some helpful information as well.

like image 36
jjnguy Avatar answered Nov 02 '22 14:11

jjnguy