JScrollPane painting white line issue in windows look and feel

211 Views Asked by At

See the GUI:

gui

Notice a white line between the container and the scrollbar:

enter image description here

Is it possible to make this line disappear?

Code:

public class WhitePixels {
    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        JFrame f = new JFrame("A");

        JPanel container = new JPanel();
        container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
        for (int i = 0; i < 15; i++) {
            container.add(new JButton("A"));
        }

        f.setLayout(new BorderLayout());

        //some attempts
        JScrollPane sp = new JScrollPane(container);
        sp.getVerticalScrollBar().setOpaque(false);
        sp.getVerticalScrollBar().setBorder(BorderFactory.createEmptyBorder());
        sp.setOpaque(false);
        f.add(sp);

        f.pack();
        f.setSize(200, 200);
        f.setLocationByPlatform(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

Windows 10, Java 1.8

1

There are 1 best solutions below

1
camickr On
sp.getVerticalScrollBar().setBorder(BorderFactory.createEmptyBorder());

I changed the above code to:

sp.getVerticalScrollBar().setBorder(BorderFactory.createLineBorder(Color.RED));

and you will see the white line is still there on the inside of the border.

This implies the line is part of the painting of the scrollbar.

I tried playing with the UIManager to change some of the color defaults:

       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
       UIManager.put("ScrollBar.thumbHighlight", Color.RED);
       UIManager.put("ScrollBar.track", Color.YELLOW);
       UIManager.put("ScrollBar.foreground", Color.YELLOW);
       UIManager.put("ScrollBar.thumb", new ColorUIResource(Color.YELLOW));

but it had no effect.

I got the above values from UIManager Defaults in case you want to try your luck.

So I would suggest the only way to get rid of the white line is to override the LAF and do your own custom painting, which is not a task I would know how to do.