Search code examples
javaswingjcombobox

JComboBox background is not painted untill hover over it


I changed background color of JComboBox but it's not painted untill you hover over it. This is just test example for demonstration an issue here. In my main app, it's visible even if I don't hover over it. Before JFrame is fully loaded I see that combo box is highlighted with white color for few miliseconds and then becomes painted with expected color.

But I think this is the same issue.

Before hover over it:

enter image description here

After hovering over it:

enter image description here

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;

public class TestComboBox {

    private static final String[] ANIMALS = new String[]{"Cat", "Mouse", "Dog", "Elephant", "Bird", "Goat", "Bear"};
    private static final Color COMBO_COLOR = new Color(71, 81, 93);

    public static class MessageComboBox extends JComboBox<String> {

        public MessageComboBox(DefaultComboBoxModel model) {
            super(model);
            setFont(new Font("Arial", Font.PLAIN, 30));
            setPreferredSize(new Dimension(350, 50));
            setRenderer(new MyRenderer());
        }
    }

    private static class MyRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                                                      int index, boolean isSelected, boolean cellHasFocus) {

            JComponent comp = (JComponent) super.getListCellRendererComponent(list,
                    value, index, isSelected, cellHasFocus);

            list.setBackground(COMBO_COLOR);
            list.setForeground(Color.WHITE);
            list.setOpaque(false);

            return comp;
        }
    }


    public static void main(String[] args) throws Exception {
        String nimbus = Arrays.asList(UIManager.getInstalledLookAndFeels())
                .stream()
                .filter(i -> i.getName().equals("Nimbus"))
                .findFirst()
                .get()
                .getClassName();

        UIManager.setLookAndFeel(nimbus);
        UIManager.put("ComboBox.forceOpaque", false);
        UIManager.put("ComboBox.padding", new Insets(3, 3, 3, 0));

        JFrame jf = new JFrame();
        jf.setSize(800, 400);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setLocationRelativeTo(null);

        MessageComboBox comboBox = new MessageComboBox(new DefaultComboBoxModel(ANIMALS));
        comboBox.setFocusable(true);
        JPanel panel = new JPanel();
        panel.add(comboBox);
        jf.add(panel, BorderLayout.NORTH);
    }
}

Can anyone please help with issue I have demonstrated in these screens?


Solution

  • I am not sure but I think if you put this lines

    list.setBackground(COMBO_COLOR);
    list.setForeground(Color.WHITE);
    list.setOpaque(false);
    

    Before this line:

    JComponent comp = (JComponent) super.getListCellRendererComponent(list,
                    value, index, isSelected, cellHasFocus);