0

I have some code running in a swingworker, because it might cause the UI to block, I would like to revise the UI with the progress of the swingworker thread so I am using publish/process to revise a JLabel, which works, but there are other UI components that I would like to revise, is this the only thread safe way to do it, through the process method?

I once got this exception when running it it, I assume it is because I was revising the UI components in the doInBackground method rather than from the process method?

java.lang.Boolean cannot be cast to javax.swing.Painter
Tim the Enchanter
  • 10,837
  • 4
  • 21
  • 20
  • Use the PropertyChange support of the SwingWorker, by attaching a PropertyChangeListener to it and monitoring for the "progress" property. Within the SwingWorker, call setProgress to update this property, [for example](http://stackoverflow.com/questions/24835638/issues-with-swingworker-and-jprogressbar/24835935#24835935) – MadProgrammer Aug 03 '14 at 22:11
  • @MadProgrammer Sorry do you mean add a PropertyChangeListener to the label? – Tim the Enchanter Aug 03 '14 at 22:21
  • No, to the `SwingWorker`, look at the linked example – MadProgrammer Aug 03 '14 at 22:32
  • @MadProgrammer Sorry if this is a silly suggestion but would enclosing the call to create and execute the swingworker in a invoke later runnable be a solution ? : `javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { FireComputerShots fireCompShots = new FireComputerShots(); // Call the swing worker thread fireCompShots.execute(); } });` – Tim the Enchanter Aug 03 '14 at 22:44
  • I'm not sure I understand, as I see it, your Swingaworker is producing an existed type of object, but you want to know the progression of this work, SwingWorker already provides progress support via the PropertyChangeListener support – MadProgrammer Aug 03 '14 at 22:47
  • Personally, wrapping within a InvokeLater call is irrelevant, regardless of which thread you start in, as the point of a SwingWorker is to execute the doInBackground method within its own Thread, outside of the Event Dispatching Thread, but proving simple functionality for synching the results back to the EDT without you needing to use SwingUtilities... – MadProgrammer Aug 03 '14 at 22:49
  • @MadProgrammer It is only creating progress information at the moment by calling publish, which is setting the value of a JLabel, I though that was how it was done. – Tim the Enchanter Aug 03 '14 at 22:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58584/discussion-between-tim-the-enchanter-and-madprogrammer). – Tim the Enchanter Aug 03 '14 at 22:52

1 Answers1

1

SwingWorker provides progress functionality via the set/getProgress methods and the support of the PropertyChangeListener.

You can attach a PropertyChangeListener to the SwingWorker and monitor for the changes to the progress property, updating the UI in the manner in which you need.

You would setProgress from within side the doInBackground method at the points you need to in order to update the progress state.

Have a look at Issues with SwingWorker and JProgressBar for an example

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366