1

Let's say i have a listener attached to a button. When i press this button, actionPerformed is called and i set a label as visible. Then the calculate() method runs(which has some really long calculations inside it and it takes time). Then i wanna print the results with the show() method.

Thing is that i know for a fact that the label will be set as visible after all the code inside actionPerformed will be executed.

So my question is : How should i set the calculate method to run on background? Threads? SwingTimer? SwingWorker? I haven't found an ideal way yet.

class ButtonListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        calculateLbl.setVisible(true);
        calculate();           
        show();
    }
}

1 Answer 1

3

Your problem is one of Swing concurrency: When calculate() is called on the Swing event thread, the long-running code hampers the event thread, preventing it from painting to the JLabel. The solution is to run calculate in a background thread, and then be notified when it is done. When notification occurs, call show(). A SwingWorker would work great for this since it comes with its own notification mechanism.

e.g.,

@Override
public void actionPerformed(ActionEvent e) {
    calculateLbl.setVisible(true);
    new SwingWorker<Void, Void>() {
        public Void doInBackground() throws Exception{
            calculate(); // this is run in a background thread
                         // take care that calculate makes no Swing calls
            return null;
        }

        protected void done() {
            show();  // this is run on the Swing event thread
        }
    }.execute();
}

Caveat: code not tested/compiled/nor run.

A problem with the above code is that it does not handle any exceptions that might be thrown within the calculate method, and a cleaner better way to do this is to create a SwingWorker variable, attach a PropertyChangeListener to it, and when its SwingWorker.StateValue is SwingWorker.StateValue.DONE, call get() on the SwingWorker and handle any possible exceptions there.

4
  • Thank you for your anwser, the sample code helped me understand how to start and now i can edit it more and go on. Mar 29, 2016 at 20:02
  • Could you tell me a way that i can call show() to print results in the frame while calculate() is running? For example it could run every 100 times of the inside the calculate loop. Mar 29, 2016 at 20:05
  • @breathtaker: Have a look at using the publish/process within your SwingWorker, including looking at the SwingWorker Tutorial as well as searching this site. For example, have a look at my related answers that can easily be found. Mar 29, 2016 at 20:07
  • @breathtaker: In particular, please check out these answer links: answer link, answer link, answer link. Mar 29, 2016 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.