1

Normally when we choose the items in jCombobox by mouse or keyborad up and down keys, the background of item will be 'blue' color.

enter image description here

I want to change the color from blue to any other color something like follows.

enter image description here

Ilham
  • 33
  • 1
  • 1
  • 6
  • 1
    i think you must set one of the swing UI properties, i'm not sure which (let us know which one work) but have a look at the list here... https://stackoverflow.com/questions/1951558/list-of-java-swing-ui-properties – Martin Frank Sep 14 '17 at 05:24
  • 1
    You can also change it by supplying a custom cell renderer – MadProgrammer Sep 14 '17 at 05:40

1 Answers1

3

Another option is to use JList#setSelectionBackground(Color).

screenshot

import java.awt.*;
import javax.accessibility.AccessibleContext;
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboPopup;

public class ComboTest {
  public JComponent makeUI() {
    // TEST:
    // UIManager.put("ComboBox.selectionBackground", Color.GREEN);
    // UIManager.put("ComboBox.selectionForeground", Color.CYAN);

    String[] model = {"Male", "Female"};

    JComboBox<String> combo1 = new JComboBox<>(model);
    AccessibleContext ac = combo1.getAccessibleContext();
    BasicComboPopup pop = (BasicComboPopup) ac.getAccessibleChild(0);
    JList list = pop.getList();
    list.setSelectionForeground(Color.WHITE);
    list.setSelectionBackground(Color.ORANGE);

    // ???: The focus border of the JComboBox is not drawn.
    JComboBox<String> combo2 = new JComboBox<>(model);
    combo2.setRenderer(new DefaultListCellRenderer() {
      @Override public Component getListCellRendererComponent(
          JList list, Object value, int index,
          boolean isSelected, boolean hasFocus) {
        JLabel l = (JLabel) super.getListCellRendererComponent(
            list, value, index, isSelected, hasFocus);
        if (isSelected) {
          l.setForeground(Color.WHITE);
          l.setBackground(Color.ORANGE);
        } else {
          l.setForeground(Color.BLACK);
          l.setBackground(Color.WHITE);
        }
        return l;
      }
    });

//     // TEST:
//     JComboBox<String> combo3 = new JComboBox<>(model);
//     combo3.setRenderer(new DefaultListCellRenderer() {
//       @Override public Component getListCellRendererComponent(
//           JList list, Object value, int index,
//           boolean isSelected, boolean hasFocus) {
//         list.setSelectionForeground(Color.WHITE);
//         list.setSelectionBackground(Color.ORANGE);
//         return super.getListCellRendererComponent(
//             list, value, index, isSelected, hasFocus);
//       }
//     });

    JPanel box = new JPanel(new GridLayout(0, 1, 10, 10));
    box.add(combo1);
    box.add(combo2);
//     box.add(combo3);

    JPanel p = new JPanel(new BorderLayout());
    p.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    p.add(box, BorderLayout.NORTH);
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
      try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        // UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
      } catch (Exception e) {
        e.printStackTrace();
      }
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new ComboTest().makeUI());
      f.setSize(320, 240);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}
aterai
  • 9,658
  • 4
  • 35
  • 44
  • can you set JList#setSelectionBackground(Color) on a combobox? i didn't understand that hint (sounds interesting though!) – Martin Frank Sep 14 '17 at 09:17
  • 1
    @MartinFrank This example use the [ComboPopup#getList()](https://docs.oracle.com/javase/8/docs/api/javax/swing/plaf/basic/ComboPopup.html#getList--) method. And `ComboPopup` can be get `protected ComboPopup popup; //BasicComboBoxUI` or `JComboBox#getAccessibleContext().getAccessibleChild(0)`. – aterai Sep 14 '17 at 09:36