JFormDesigner / FlatLaf

FlatLaf - Swing Look and Feel (with Darcula/IntelliJ themes support)
https://www.formdev.com/flatlaf/
Apache License 2.0
3.42k stars 272 forks source link

JScrollPane Focused Border Bug #876

Closed poce1don closed 3 months ago

poce1don commented 3 months ago

Karl,

See this bug with the JScrollPane border. Test 1 Test 2 Test 3
shot1 shot2 shot3

When the rendered cells of the JList are JPanel, the rendered content is "invading" the place intended for the border, only when the scrolling is at the beginning, in other parts, the problem does not occur.

The bug only occurs at 1.25x scale.

If the cells are rendered as standard, the bug does not occur.

The tested FlatLaf versions were 3.5 and 3.6-SNAPSHOT.

Using HiDPIUtils.installHiDPIRepaintManager() also did not solve the bug.

public class ListFail extends JFrame {

    public static void main(String[] args) {

        System.setProperty( "sun.java2d.uiScale", "125%" );

        SwingUtilities.invokeLater(() -> {
            FlatDarkLaf.setup();
            ListFail app = new ListFail();
            app.setVisible(true);
        });

    }

    public ListFail() {
        super("JScrollPane Focused Border Fail");

        String style = "focusedBorderColor: #FF0000;";

        DefaultListModel<String> listModel1 = new DefaultListModel<>();
        JList<String> list1 = new JList<>(listModel1);
        list1.setCellRenderer(new CustomRender());
        JScrollPane scroll1 = new JScrollPane(list1);
        scroll1.putClientProperty(FlatClientProperties.STYLE, style);

        DefaultListModel<String> listModel2 = new DefaultListModel<>();
        JList<String> list2 = new JList<>(listModel2);
        JScrollPane scroll2 = new JScrollPane(list2);
        scroll2.putClientProperty(FlatClientProperties.STYLE, style);

        for ( int i = 0; i < 15; i++ ) {
            listModel1.addElement("Message ID " + i);
            listModel2.addElement("Message ID " + i);
        }

        JPanel panel = new JPanel(new GridLayout(1, 2, 10, 0));
        panel.setBorder(new FlatEmptyBorder(10,10,10,10));
        panel.add(scroll1);
        panel.add(scroll2);

        add(panel, BorderLayout.CENTER);
        setSize(400, 220);
        setLocationRelativeTo(null);

    }

    private static class CustomRender
            extends JPanel
            implements ListCellRenderer<String> {

        private final JLabel lblText;
        private final JPanel pnlContent;

        public CustomRender() {
            super(new BorderLayout());

            lblText = new JLabel();
            pnlContent = new JPanel();
            pnlContent.add(lblText);

            add(pnlContent, BorderLayout.CENTER);
            setBorder(new FlatEmptyBorder(4,4,0,4));
            setBackground(ColorFunctions.shade(UIManager.getColor("List.background"), 0.45f));

        }

        @Override
        public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus) {

            lblText.setText(value);
            pnlContent.setBackground(isSelected ? UIManager.getColor("List.selectionBackground") : UIManager.getColor("List.background"));

            return this;

        }

    }

}
DevCharly commented 3 months ago

Seems to be a similar (or same) clipping bug in Swing as described in PR #864.

It is also in Metal and Nimbus L&F (you need to change empty border of panel to 9,10,10,10):

image

image

poce1don commented 3 months ago

This Swing scales part has a lot of bugs on Windows. At least in this case, the solution is simple. Thanks for the tip! 👍

DevCharly commented 3 months ago

... At least in this case, the solution is simple. Thanks for the tip! 👍

And what is the solution 🤔

poce1don commented 3 months ago

It is also in Metal and Nimbus L&F (you need to change empty border of panel to 9,10,10,10):

Your tip solved my problem.

Using ScrollPane.viewportBorder also solves this screenshot error, in fact that's what I'm using and the border is being painted without errors at any scale.