JComboBox在每个项目中都有不同的颜色失败

时间:2011-06-14 03:33:00

标签: java swing jcombobox renderer

  • 目的:使JComboBox具有不同的背景颜色和文本 在每个项目中。
  • 我的问题:背景颜色不会改变,文本不是我在setText中设置的,它已在System.out.println中正确显示。 getSelectedIndex()效果很好。

捕获:http://i.stack.imgur.com/EgfZs.png

以下是我已经消化并尝试错误Dr.Google显示的代码:

public class ColorCode{
   private Color color;
   private String alias;
   ...
}
public class ElectronicColorCode extends JFrame implements ActionListener{
   private JComboBox[] selections = new JComboBox[4];
   ...
   public ElectronicColorCode(){
      for(int i=0; i<selections.length; i++){
         selections[i] = new JComboBox();
         for(int j=0; j<tolColorSets.length; j++)
            selections[i].addItem(new ComboBoxRenderer(colorSets[j]));
      }
      selections[i].addActionListener(this);
      ...
   }
}
class ComboBoxRenderer extends JLabel implements ListCellRenderer{
   private ColorCode colorCode;

   public ComboBoxRenderer(ColorCode colorCode){
      super();
      this.colorCode = colorCode;
      setBackground(colorCode.getColor());
      setText(colorCode.getAlias());
      System.out.println(colorCode.getAlias());
   }

   public Component getListCellRendererComponent(JList list, Object obj, int row, boolean isSelected, boolean hasFocus){
      return this;
   }
}

1 个答案:

答案 0 :(得分:6)

您不会将渲染器添加为组合框的项目。渲染器用于渲染存储在模型中的Object。如果需要,可以向模型添加自定义对象,该对象包含要在渲染器中显示的文本和背景颜色。

这是一个简单的示例,说明如何执行此操作。您显然需要自定义代码以存储和呈现背景颜色而不是id。

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class ComboBoxItem2 extends JFrame implements ActionListener
{
    public ComboBoxItem2()
    {
        Vector model = new Vector();
        model.addElement( new Item(1, "car" ) );
        model.addElement( new Item(2, "plane" ) );
        model.addElement( new Item(4, "boat" ) );
        model.addElement( new Item(3, "train" ) );

        JComboBox comboBox = new JComboBox( model );
        comboBox.setRenderer( new ItemRenderer() );
        comboBox.addActionListener( this );
        getContentPane().add(comboBox, BorderLayout.SOUTH );
    }

    public void actionPerformed(ActionEvent e)
    {
        JComboBox comboBox = (JComboBox)e.getSource();
        Item item = (Item)comboBox.getSelectedItem();
        System.out.println( item.getId() + " : " + item.getDescription() );
    }

    class ItemRenderer extends BasicComboBoxRenderer
    {
        public Component getListCellRendererComponent(
            JList list, Object value, int index,
            boolean isSelected, boolean cellHasFocus)
        {
            super.getListCellRendererComponent(list, value, index,
                isSelected, cellHasFocus);

            Item item = (Item)value;
            setText( item.getDescription().toUpperCase() );

            return this;
        }
    }

    class Item
    {
        private int id;
        private String description;

        public Item(int id, String description)
        {
            this.id = id;
            this.description = description;
        }

        public int getId()
        {
            return id;
        }

        public String getDescription()
        {
            return description;
        }

        public String toString()
        {
            return description;
        }
    }

    public static void main(String[] args)
    {
        JFrame frame = new ComboBoxItem2();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
     }

}