问题
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 current value to 6 then nothing will be displayed. The progress bar is empty. If I put 7 as current value then it works. It seems to be a problem with any empty border or other space. The problem occurs with Windows 7 and only if the UIManager is set to SystemLookAndFeel.
Does anyone knows this problem and has a solution for this? Below is my code:
package lab;
import java.awt.FlowLayout;
import javax.swing.JDialog;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import core.Config;
public class ProgressSample extends JDialog {
public ProgressSample() {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
getContentPane().setLayout(new FlowLayout());
// Nothing is displayed
JProgressBar progressSix = new JProgressBar(0, 100);
progressSix.setValue(6);
getContentPane().add(progressSix);
// This works
JProgressBar progressSeven = new JProgressBar(0, 100);
progressSeven.setValue(7);
getContentPane().add(progressSeven);
pack();
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
ProgressSample dialogTest = new ProgressSample();
dialogTest.setVisible(true);
}
}
回答1:
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).
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/14560680/jprogressbar-low-values-will-not-be-displayed