I am trying to use SwingWorker to perform a lengthy task and update JLabel with the result:
button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { new SwingWorker<String, Void>() { @Override protected String doInBackground() throws Exception { return doCalculation(); } protected void done() { try { label.setText(get()); } catch (InterruptedException e) { System.out.println("thread was interrupted"); } catch (ExecutionException e) { System.out.println("there was an ExecutionException"); } } }.execute(); } });
I can press the button many times as I like, and Gui will remain responsive until two threads are completed, after which Gui freezes while the threads are running. If I only run one thread at a time, this problem still occurs.
It would be great if someone could indicate if I am using SwingWorker incorrectly or if there is another issue that I do not know about. Thank you for your time. Ian
Edit
DoCalculation () is just a lot of time:
private String doCalculation() { for (int i = 0; i < 10000000; i++) { Math.pow(3.14, i); } return threads++ + ""; }
source share