3

The progress bar is created by the following code

    bar = new JProgressBar(JProgressBar.VERTICAL);
    bar.setEnabled(true);

    bar.setBackground(Color.YELLOW);
    bar.setForeground(Color.GREEN);

    bar.setStringPainted(true);     

    getReflectedComponent().setLayout(new BorderLayout());
    getReflectedComponent().add(bar, BorderLayout.CENTER);

Note that getReflectedComponent() returns a JComponent. In another method the displayed String the max value and the current value are manipulated, not more.

I really have no idea why this very strange grey stripes are displayed at the top. If I display the progress bar horizontally they are not there...

This is how my progress bar looks like

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Florian
  • 388
  • 2
  • 3
  • 13

2 Answers2

3

I don't observe your problem with your code so it must come from something else that you are not showing us (one more argument to provide an SSCCE)

Here is an example to get yourself started:

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

public class TestScrollbars {

    protected void initUI() {
        final JFrame frame = new JFrame();
        frame.setTitle(TestScrollbars.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JProgressBar bar = new JProgressBar(JProgressBar.VERTICAL);
        bar.setEnabled(true);

        bar.setBackground(Color.YELLOW);
        bar.setForeground(Color.GREEN);

        bar.setStringPainted(true);
        bar.setString("2000 g");
        bar.setValue(65);
        frame.setLayout(new BorderLayout());
        frame.add(bar, BorderLayout.CENTER);
        frame.setSize(500, 400);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestScrollbars().initUI();
            }
        });
    }

}

And the resulting image (where progress bar is correctly displaying the chosen colors):

result

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
2

Also consider a custom ProgressBarUI, mentioned here and shown here.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045