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

假装没事ソ 提交于 2019-12-04 01:18:47

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/977740/swing-cant-get-jbutton-to-update-repaint-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!