如何设置JComboBox选中项的背景色,请高手赐教,谢谢

解决方案 »

  1.   

    自定义JComboBox的单元绘制器,初始化JComboBox时setRenderer(new MyCellRenderer()); class MyCellRenderer extends JLabel implements ListCellRenderer {
         public MyCellRenderer() {
             setOpaque(true);
         }     public Component getListCellRendererComponent(JList list,
                                                       Object value,
                                                       int index,
                                                       boolean isSelected,
                                                       boolean cellHasFocus) {         setText(value.toString());         Color background;
             Color foreground;         // check if this cell represents the current DnD drop location
             JList.DropLocation dropLocation = list.getDropLocation();
             if (dropLocation != null
                     && !dropLocation.isInsert()
                     && dropLocation.getIndex() == index) {             background = Color.BLUE;
                 foreground = Color.WHITE;         // check if this cell is selected
             } else if (isSelected) {
                 background = Color.RED;
                 foreground = Color.WHITE;         // unselected, and not the DnD drop location
             } else {
                 background = Color.WHITE;
                 foreground = Color.BLACK;
             };         setBackground(background);
             setForeground(foreground);         return this;
         }
     }