Skip to content

Crash in combobox with custom renderer after switching to FlatLaf #16

Closed
@kirill-grouchnikov

Description

@kirill-grouchnikov
public class Test {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(new NimbusLookAndFeel());
                } catch (Throwable t) {
                    t.printStackTrace(System.err);
                }

                JComboBox comboColors = new JComboBox<>(new Color[] {new Color(255, 128, 128),
                        new Color(128, 255, 128), new Color(128, 128, 255), new Color(255, 255, 128),
                        new Color(255, 128, 255), new Color(128, 255, 255)});

                comboColors.setRenderer(new DefaultListCellRenderer() {
                    @Override
                    public Component getListCellRendererComponent(JList list,
                            Object value, int index, boolean isSelected,
                            boolean cellHasFocus) {
                        Color color = (Color) value;
                        Component result = super.getListCellRendererComponent(list,
                                color.getRed() + ":" + color.getGreen() + ":" + color.getBlue(),
                                index, isSelected, cellHasFocus);
                            result.setBackground(color);
                        return result;
                    }
                });

                JButton toFlat = new JButton("Set FlatDark!");
                toFlat.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        SwingUtilities.invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    UIManager.setLookAndFeel("com.formdev.flatlaf.FlatDarkLaf");
                                    for (Window w : Window.getWindows()) {
                                        SwingUtilities.updateComponentTreeUI(w);
                                    }
                                } catch (Throwable t) {
                                    t.printStackTrace(System.err);
                                }
                            }
                        });
                    }
                });

                JFrame frame = new JFrame("Combo test");
                frame.setLayout(new BorderLayout());
                frame.add(comboColors, BorderLayout.NORTH);
                frame.add(toFlat, BorderLayout.SOUTH);
                frame.setSize(400, 400);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

Click the "Set FlatDark!" button and then click the combo at the top. Result:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at java.desktop/javax.swing.plaf.basic.BasicComboPopup.getPopupHeightForRowCount(BasicComboPopup.java:1283)
	at java.desktop/javax.swing.plaf.basic.BasicComboPopup.getPopupLocation(BasicComboPopup.java:1369)
	at java.desktop/javax.swing.plaf.basic.BasicComboPopup.show(BasicComboPopup.java:234)
	at java.desktop/javax.swing.plaf.basic.BasicComboPopup.togglePopup(BasicComboPopup.java:1218)
	at java.desktop/javax.swing.plaf.basic.BasicComboPopup$Handler.mousePressed(BasicComboPopup.java:897)
	at java.desktop/java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:287)
	at java.desktop/java.awt.Component.processMouseEvent(Component.java:6633)

This is on 0.14

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions