JProgressBar: low values will not be displayed

前端 未结 3 1430
感情败类
感情败类 2020-12-20 18:04

I tried out the function of the JProgressBar in Java. But there is a problem I couldn\'t solve:

When I set the minimum to zero, the maximum value to 100 and the curr

相关标签:
3条回答
  • 2020-12-20 18:30

    It looks like a layout issue / bug when setting the system L&F.

    I managed to hide the bug by setting a preferred size to the progress bar with the issue:

    progressSix.setPreferredSize(new Dimension(200, 15));
    

    It seems to be related to the progress bar size. If I use a BoxLayout instead of the FlowLayout and resize the container enough, I can see it displaying even for smaller values.

    0 讨论(0)
  • 2020-12-20 18:32

    The java code for a Windows native look-and-feel progress bar renders using PROGRESSCHUNKSIZE steps in the manner of the original windows progress bar. Please see the source for the Windows JProgressBar.

    It's just not rendering it smoothly. If you step the progress bar you can see the chunks.

    It may be customizable, but I don't know how you would accomplish it.

    Origin of the Issue

    The original windows XP progress bar was in little boxes. The theme defined a size of the box, and the gap between the box. For Vista and later, the theme was changed to specify the gap between the box as 0, but never reset the size of the box to 1 pixel. Reading the value through the OpenThemeData and GetThemeInt Win32API functions reveals that the chunk size is 6 for my theme (Windows 8).

    0 讨论(0)
  • 2020-12-20 18:35

    The progress indicator becomes smooth after you set:

    progressBar.setStringPainted(true);
    

    It however now shows a percentage, and the color changed to blue. You can do this:

    progressBar.setString(""); // null means automatic percentage
    progressBar.setForeground(new Color(0, 210, 40)); // Windows7-green
    

    I think this works because the bar is now drawn (partially) by SWING instead of by the OS. The bar looks different as the progress indicator is missing the glass-like highlight and is painted solid. The background still looks the same. My guess is that SWING lets the OS draw it as if the progress is 0% and then draws a filled rectangle and the text on top.

    The last setting, the color, could possibly be improved by querying a UI constant. However, within all the ProgressBar.* keys of the WindowsLookAndFeel, I could only find the blue colors.

    0 讨论(0)
提交回复
热议问题