Florian
Florian

Reputation: 388

Vertical progress bar has strange background which is not changeable

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

Upvotes: 3

Views: 1094

Answers (2)

trashgod
trashgod

Reputation: 205865

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

image

Upvotes: 2

Guillaume Polet
Guillaume Polet

Reputation: 47607

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

Upvotes: 3

Related Questions