简体   繁体   English

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

[英]JComboBox having different color in every item failed

  • Purpose : to have a JComboBox with different background colors and text in each item.目的:在每个项目中拥有一个具有不同背景 colors 和文本的 JComboBox。
  • My problem : The background color doesn't change, and the text is not what I've set in setText, which have been correctly shown in System.out.println.我的问题:背景颜色没有改变,文本不是我在setText中设置的,在System.out.println中已经正确显示。 The getSelectedIndex() works well. getSelectedIndex() 效果很好。

The capture: http://i.stack.imgur.com/EgfZs.png截图: http://i.stack.imgur.com/EgfZs.png

The following is the code after I've digested and try-and-error what Dr.Google shows:以下是我消化并尝试错误后的代码 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;
   }
}

You don't add renderers as an item for the combo box.您不会将渲染器添加为组合框的项目。 The renderer is used to renderer the Object that is stored in the model.渲染器用于渲染存储在 model 中的 Object。 If you want you can add a custom object to the model that contains both the text and the background color you want displayed in the renderer.如果需要,可以将自定义 object 添加到 model 中,其中包含要在渲染器中显示的文本和背景颜色。

Here is a simple example that shows how to do this.这是一个简单的例子,展示了如何做到这一点。 You will obviously need to customize the code to store and render the background color instead of the id.您显然需要自定义代码来存储和呈现背景颜色而不是 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 );
     }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM