【问题标题】:JComboBox having different color in every item failedJComboBox 在每个项目中都有不同的颜色失败
【发布时间】:2011-06-14 03:32:59
【问题描述】:
  • 目的:拥有一个具有不同背景颜色和文本的 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;
   }
}

【问题讨论】:

    标签: java swing jcombobox renderer


    【解决方案1】:

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

    这里有一个简单的例子来说明如何做到这一点。您显然需要自定义代码来存储和呈现背景颜色而不是 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 );
         }
    
    }
    

    【讨论】:

    • 我之前尝试过这个版本,jvm 向我抱怨 Object 值无法转换为 Item。
    • ...这是由我的程序中的错误引起的...感谢您的指导
    猜你喜欢
    • 1970-01-01
    • 2012-06-12
    • 2016-07-13
    • 2017-05-19
    • 2017-01-17
    • 1970-01-01
    • 2020-11-09
    • 2017-07-26
    相关资源
    最近更新 更多