Why is SwingWorker # setProgress limited to [0, 100]?

SwingWorker#setProgress throws an IllegalArgumentException if the argument is not 0 to 100. I thought the main purpose of the setProgress method was to update a JProgressBar (as it was in this guide ). If so, why limit SwingWorker progress to [0, 100] when JProgressBar progress is unlimited?

+7
source share
4 answers

Is there a good reason that they limited progress to [0,100]?

To the extent that this question has a constructive answer, the concept of what constitutes progress is probably best known for what is modeled in the background thread. Scaling limits the number of parts that need to be exposed.

SwingWorker meant to be a subclass at the same time. Nothing prevents the addition of the setProgress(int progress, in min, int max) method setProgress(int progress, in min, int max) and support for the properties of a companion property, but it’s hard to see any advantage - the recipient just needs to scale it to display.

+4
source

The discussion of api design is quite interesting, but apt to guess :-)

Some random thoughts at the end of the week:

  • The basic SwingWorker process model allows you to do something in the background and maintain reporting of both the intermediate and final result / s back to the EDT.

  • it is intended for a subclass (as @trashgod already emphasized), at the same time it is trying to minimize the efforts required for this: the state and progress are fully defined and implemented by convenient properties intended for use as,

  • therefore, api doc rules, which clearly indicate the permissible values ​​of the process of the associated property, which are in the range 0 ... 100, incusively. No ambiguity, nothing can be changed in user implementations. Percentage is the most common use case, keeping it out of the box is a reasonable thing.

  • To present intermediate results in coordinates other than percent, the intented way is to implement the process (..). It depends on the user implicating what it is doing, it can launch a user defined property or populate a view model directly or ... no matter what

  • Finally, an example tutorial is just an example. Furthermore, this is incompatible in itself: a description describing the task.getLenghtOfTask method, which is not implemented in the custom swingworker. My (wild) guess is that the sample text is still based on an older version that could support arbitrary values.

To answer the question (my 0.02Euro cents :-) - nothing wrong with the design, this is a balanced basic implication that handles common use cases outside the box and at the same time flexible enough to make the more advanced requirement easy to implement.

for updating when one piece of information is available

you cannot completely control the granularity of reporting: all you can do is determine the unit of the smallest fragment, and then expect to get one or more of these smallest coins.

+8
source

1) SwingWorker#setProgress ---> I accept this as a percentage, basically there is a range between 0 and 100, agreed that algebra knows negative% or 1M at%, but in the same way you can calculate the fourth / fifth dimension, a little harder may be with the Drake equation , but maybe

2) JProgressBar ---> does not have these restrictions / limitations, because you can write good code where you can switch (JComboBox) the betweens value in%, Mb, stay .... for progress

+2
source

It depends on your use case, but you can also use the publish method to publish progress and update the progress bar in the publish method. See my answer to another SO question for an example

+2
source

All Articles