I wrote a code for a gui program.
In this program there is a SwingWorker to find out prime numbers until a fixed number. Every prime number is published and printed. During the computation progress is update, so is launched an event PropertyChangeEvent.
The ChangePropertyListener have to update a JProgressBar with the progress value of SwingWorker.
In the ChangePropertyListener i don't understand how get the value of progress between :
Could someone tell me the differences and explain what do getNewValue() (i've yet watched the Doc, but it isn't so expressive ) ?
The two of them serve different purposes. Calling SwingWorker.getProgress returns the current progress of the SwingSorker instance as the name already tells. When sticking to the contract of SwingWorker.setProgress(int) the corresponding getter always returns a value between 0 and 100. You can call this method any time you like and you'll always get the worker's most recent state. Any client code knowing the specific SwingWorker instance may call the method any time.
On the other hand there is PropertyChangeEvent.getNewValue() which returns the same value as getProgress does. In order to get this method called you have to first register a PropertyChangeListener by calling SwingWorker.addPropertyChangeListener(PropertyChangeListener). Once such a listener is registered it will receive updates concerning the workers progress. Now there are three subtle differences:
In summary, use getProgress whenever you want to get a workers progress on a pull basis, i.e. actively requesting the value. Use the PropertyChangeListener approach whenever you want to be informed about progress changes and getting not informed about every change of the progress is acceptable for you.