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

William picture William · Jun 10, 2009 · Viewed 8.3k times · Source

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.

Answer

Michael Myers picture Michael Myers · Jun 10, 2009

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();
}