several times change label text by clicking button in swing not work

前端 未结 2 1962
野的像风
野的像风 2021-01-26 07:55

I need to change jlabel.text several times by one click button in swing. In this code i need set label text to start before dowork() function and set to in progress in middle a

2条回答
  •  借酒劲吻你
    2021-01-26 08:11

    I would try to use a SwingWorker instance (in particular its doInBackground method) to do what you are currently doing in doWork on a different thread than the main UI thread. The way it's written, your listener method is bound to freeze the user interface during execution, which, as you said, can be a long time, resulting in a bad user experience.

    Change to JLabel text can happen in three different places: first, just before invoking execute on (i.e. starting) the SwingWorker; second, using the publish/process mechanism that SwingWorker makes available to publish intermediate results on the user interface; third, in the done method, which is called again on the UI thread as soon as the SwingWorker has finished the execution of its doInBackground method.

    References: Oracle's tutorial on worker threads and SwingWorker, JavaDoc API documentation of the SwingWorker class.

提交回复
热议问题