java - How to update a progress bar from a method inside SwingWorker -
I am trying to update a progress bar and I can not do it. My code is something like this:
The public class extends MyWorker SwingWorker & lt; Zero, zero> {Public Voice Doine Background () {howMany = Integer.parseInt (textField.getText ()); String result = long method (how many); Label.setText ("Hello, you have" + results); }} The implementation of the public square event ActionListener {Public Zero Action Functional (ActionEvent E) {label2.setText ("whatever"); Button.setEnabled (wrong); MyWorer.addPropertyChangeListener (this); MyWorker.execute (); } Change the Public Zero Property (Change Property Event Event) {If ("Progress" .equals (event.getPropertyName ())} {int currentPercent = (int) event.getNewValue (); ProgressBar.setValue (currentPercent); }}} Therefore longMethod () in setprogress in DoInBackground can not be used is a method in which there is a very slow loop, which is placed in the second class. I have passed such a method in any way that sends a variable in that category which contains JFrame and then gives the possibility of seeing that progress if you click on another button Click. I do not know there are some ways to make that button (or text field), refresh every X seconds without clicking or using setProgress method A method for longMethod () Thanks!
What do you need long method to return progress information You can, for example, create a simple interface , which you can pass through long method , when it knows, progress Update ... Public Interface Progress Monitor {/ ** * 0-1 * / Passed progress between public Zero Progress (Dual Progress); } Then in your doInBackground method, you will give an example of ProgressMonitor to longMethod . The public class extends MyWorker SwingWorker & lt; Integer, integer & gt; {It's better to get this value before public integer doInBackground () {// // // Background is called, but it's just me ... howMany = Integer.parseInt (textField.getText ()); String result = long method (how, new progress monitor) (public zeros progress renewed (double progression) {set progress ((int) (progress * 100))}}}); //label.setText ("Hello, you have" results); Published (results); Return result; } Secure zero process (list & lt; integer & gt; section) {label.setText ("Hello, you have" + chunks.get (chunks.size () - 1)); }} This is an example of
specially, if you can not modify longMethod , then you have any of its It does not mean that you can update progress, because you have no way of knowing what longMethod is doing ...
Comments
Post a Comment