3

Hello i would like to know if i can extends a JProgressBar to use double value for min max, instead of int. Thank you.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
wotan2009
  • 151
  • 1
  • 3
  • 6

2 Answers2

6

If you know the bounds and precision of your values, it's quite easy to do with a wrapper. I've used this approach for a computationally intensive application where 1% equates to about 30 seconds. Showing a JProgressBar with min=0 and max=100 didn't give enough feedback. Users thought the application was hung.

Solution: Scale the values

I had a float (0f - 1f) that represented the actual percent complete. By multiplied that by 10000 I got an integer range of 0 - 10000. I set up JPB with min=0 and max=10000.

Then just a call to setString(NumberFormat.getPercentInstance().format(value)) to display the formatted percent.

Sufian
  • 6,405
  • 16
  • 66
  • 120
Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71
4

Sure you can, but for what purpose? How do you display a half percent?

You can however use the setString()-method to display the more exact value. Printing the String must be turned on using the setStringPainted()-method.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • If your progress bar is 400 pixels wide, you show half a percent as two pixels. It gives fine-grained resolution when you have more information. – Kieveli Oct 10 '13 at 14:56
  • @Kieveli i see your point, but this works with normal integers as well. If you want the progressbar to be the most fine-grained, you set the maximum to your highest possible value. The bar will take care of drawing as fine as it can. – Lukas Knuth Oct 10 '13 at 16:41
  • I believe I was missing the point of setting the bar from 0-10000 instead of 0-100. Effectively, 0-10000 gives you potential render precision of two decimal places - assuming you have a crazy wide pixel count. – Kieveli Oct 18 '13 at 17:48