JComboBox – Renderers and Editors


Set Renderer that uses JLabel

jComboBox1.setRenderer(new ListCellRenderer() {
    public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
        JLabel label = new JLabel();
        label.setPreferredSize(new Dimension(300, 100));
        if (value != null) {
            label.setOpaque(true);
            if (isSelected) {
                label.setBackground(Color.GREEN.darker());
            } else {
                label.setBackground(Color.GREEN);
            }
            label.setText(value.toString());
        }
        return label;
    }
});
jComboBox1.repaint();
Reset:
    ComboBoxUI tempUI = jComboBox1.getUI();
    jComboBox1.setRenderer(null);
    jComboBox1.setUI(tempUI);

Note:
    Opaque gets reset to false for the Selected Item.
    Put JLabel in a JPanel to display background color
      of the primary display spot (next to the arrow). 

Set Renderer that uses JPanel

jComboBox1.setRenderer(new ListCellRenderer() {
    public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(300, 50));
        panel.setLayout(new BorderLayout());
        JLabel label = new JLabel();
        label.setOpaque(true);
        Color color = Color.GREEN;
        if (index == -1) {
            color = Color.RED;
        }
        if (value != null) {
            if (value instanceof Color) {
                color = (Color) value;
            }
            label.setText(value.toString());
        }
        if (isSelected) {
            label.setBorder(new LineBorder(color.darker(), 10));
            label.setBackground(color.darker());
        } else {
            label.setBorder(new LineBorder(color, 10));
            label.setBackground(color);
        }
        panel.add(label);
        return panel;
    }
});
jComboBox1.repaint();
Reset:
    ComboBoxUI tempUI = jComboBox1.getUI();
    jComboBox1.setRenderer(null);
    jComboBox1.setUI(tempUI);

Note:
    An index of -1 (passed to getListCellRendererComponent) is the Item
      to be drawn in the primary display spot (next to the arrow).

Allow Editing

jComboBox1.setEditable(true);
jComboBox1.revalidate();
jComboBox1.repaint();

Reset:
    jComboBox1.setEditable(false);
    jComboBox1.revalidate();
    jComboBox1.repaint()

Request Editor Focus

if (jComboBox1.getEditor() != null) {
    jComboBox1.getEditor().getEditorComponent().requestFocus();
}

Select All Editor

if (jComboBox1.getEditor() != null) {
    jComboBox1.getEditor().selectAll();
}

Note:
    Also requests focus

Set Editor Value

jComboBox1.configureEditor(jComboBox1.getEditor(), "New Editor Value");

Set Add-At-End Text Editor

BasicComboBoxEditor bce = new BasicComboBoxEditor();
bce.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        jComboBox1.addItem(bce.getItem());
    }
});
jComboBox1.setEditor(bce);
Reset:
    ComboBoxUI tempUI = jComboBox1.getUI();
    boolean isEditable = jComboBox1.isEditable();
    jComboBox1.setEditable(false);
    jComboBox1.setEditor(null);
    jComboBox1.setUI(tempUI);
    jComboBox1.setEditable(isEditable);

Set Color Chooser Button Editor

BasicComboBoxEditor bce = new BasicComboBoxEditor() {
    JButton button = null;

    @Override
    public Component getEditorComponent() {
        if (button == null) {
            button = new JButton("Pick Color");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Color color = JColorChooser.showDialog(jComboBox1, "Pick Color", Color.WHITE);
                    if (color != null) {
                        jComboBox1.addItem(color.toString());
                    }
                }
            });
        }
        return button;
    }
};
Reset:
    ComboBoxUI tempUI = jComboBox1.getUI();
    boolean isEditable = jComboBox1.isEditable();
    jComboBox1.setEditable(false);
    jComboBox1.setEditor(null);
    jComboBox1.setUI(tempUI);
    jComboBox1.setEditable(isEditable);