【问题标题】:Change JCombobox item color when choose by keyboard or mouse通过键盘或鼠标选择时更改 JCombobox 项目颜色
【发布时间】:2017-09-14 05:20:23
【问题描述】:

通常当我们通过鼠标或键盘上下键选择jCombobox中的项目时,项目的背景会是'蓝色'。

我想将颜色从蓝色更改为任何其他颜色,如下所示。

【问题讨论】:

  • 我认为您必须设置一个摆动 UI 属性,我不确定哪个(让我们知道哪个有效)但请查看此处的列表...stackoverflow.com/questions/1951558/…跨度>
  • 您也可以通过提供自定义单元格渲染器来更改它

标签: java swing jcombobox


【解决方案1】:

另一种选择是使用JList#setSelectionBackground(Color)

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);
    });
  }
}

【讨论】:

  • 你能在组合框上设置 JList#setSelectionBackground(Color) 吗?我不明白那个提示(虽然听起来很有趣!)
  • @MartinFrank 此示例使用ComboPopup#getList() 方法。而ComboPopup 可以得到protected ComboPopup popup; //BasicComboBoxUIJComboBox#getAccessibleContext().getAccessibleChild(0)
猜你喜欢
  • 2011-12-05
  • 2016-11-07
  • 1970-01-01
  • 1970-01-01
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多