2011-06-14 85 views
0
  • 目的:有一個JComboBox,有不同的背景顏色,並在每個項目的文本 。
  • 我的問題:背景色不改變,並且文本不是我在setText中設置的,它已在System.out.println中正確顯示。 getSelectedIndex()運行良好。

捕獲:http://i.stack.imgur.com/EgfZs.png具有不同的顏色中的每一項的JComboBox失敗

以下是我的代碼已經消化,並嘗試和錯誤是什麼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; 
    } 
} 

回答

6

你不加渲染的一個組合框的項目。渲染器用於渲染存儲在模型中的對象。如果需要,可以將自定義對象添加到包含要在呈示器中顯示的文本和背景顏色的模型。

下面是一個簡單的例子,說明如何做到這一點。您顯然需要自定義代碼來存儲和呈現背景顏色而不是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); 
    } 

} 
+0

我以前試過這個版本,jvm向我抱怨Object值不能轉換成Item。 – 2011-06-14 05:57:07

+0

@Asakura凱塔爲我工作,@camickr +1 – mKorbel 2011-06-14 07:27:49

+0

...這是由我的程序中的錯誤引起的...感謝您的指導 – 2011-06-14 08:38:02