2011-02-15 74 views
7

JComponents可以使用setName()getName()獲取隱藏數據,對不對? JComboBox物品怎麼樣? (我指的是項目在JComboBox中,而不是本身的JComboBox)Java:Swing JComboBox,是否有可能爲列表中的每個項目隱藏數據?

如果我有一個JComboBox中,和裏面我的用戶名(例如)的名單,現在我想擁有的東西像列表中每個用戶名的'id'一樣,根據它們的排列方式,最好的方法是什麼?

回答

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

public class ComboBoxItem extends JFrame implements ActionListener 
{ 
    public ComboBoxItem() 
    { 
     Vector model = new Vector(); 
     model.addElement(new Item(1, "car")); 
     model.addElement(new Item(2, "plane")); 
     model.addElement(new Item(3, "train")); 
     model.addElement(new Item(4, "boat")); 
     model.addElement(new Item(5, "boat aadf asfsdf a asd asd")); 

     JComboBox comboBox; 

     // Easiest approach is to just override toString() method 
     // of the Item class 

     comboBox = new JComboBox(model); 
     comboBox.addActionListener(this); 
     comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE); 
     getContentPane().add(comboBox, BorderLayout.NORTH); 

     // Most flexible approach is to create a custom render 
     // to diplay the Item data 

     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); 

      if (value != null) 
      { 
       Item item = (Item)value; 
       setText(item.getDescription().toUpperCase()); 
      } 

      if (index == -1) 
      { 
       Item item = (Item)value; 
       setText("" + item.getId()); 
      } 


      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 ComboBoxItem(); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

} 
+0

感謝您的回答,確實解決了我的問題:)但是,這不是使用不推薦使用的代碼「Vector」不好?有沒有辦法避免使用矢量? – evilReiko 2011-03-03 10:05:20

+1

向量不被棄用(至少在JDK6中)。 Swing組件使用模型來存儲數據。 DefaultComboBoxModel使用一個Vector來存儲數據。如果你不喜歡這個,你總是可以創建你自己的模型,並使用你想要存儲數據的任何東西。 – camickr 2011-03-03 16:10:08

3

創建User類,其具有的屬性usernameid; .toString()

+1

重寫toString爲了在JSomething中顯示是**不是推薦的方法。有關正確的方法,請參閱@camickr的答案。 – kleopatra 2012-11-01 10:13:03

4

你的對象:

public class Item { 

    private int id; 
    private String name; 

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

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String toString(){ 
     return this.name; 
    } 
} 

項目添加到您的JComboBox:

JComboBox combo; 

combo.addItem(new Item(1, "Test")); 
combo.addItem(new Item(15,"Test 2")); 

並獲得項目:

Item selected_item = (Item) combo.getSelectedItem(); 

System.out.println(selected_item.getId()); 
System.out.println(selected_item.getName()); 
相關問題