2010-08-10 81 views
3

我有一個表有兩列 - iddesc。是否有可能在JComboBox中向用戶顯示desc列值的列表,並且當用戶從列表中選擇一個項目並單擊某個按鈕時,我會得到相應的id值?JComboBox提交價值喜歡HTML選擇

也就是說,在Swing中可以這樣做嗎?

<select> 
<option value="1">Hi</option> 
<option value="2">Hello</option> 
</select> 

當用戶點擊Hello時,我需要2作爲值。

回答

4

以下是使用JComboBox的示例。這個概念是一個JList相同:

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

     JComboBox comboBox; 

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

     comboBox = new JComboBox(model); 
     comboBox.setDragEnabled(true); 
     comboBox.addActionListener(this); 
     getContentPane().add(comboBox, BorderLayout.NORTH); 

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

     comboBox = new JComboBox(model); 
     comboBox.setDragEnabled(true); 
     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); 
    } 

} 

或者你總是可以使用一個JTable來存儲數據,只是刪除第一個TableColumn的來回的TableColumnModel所以它不是在表中可見。

+0

哇...在SO camickr!剛注意到.. !! – 2010-08-10 06:57:28

2

您可以將任何對象傳遞給您的JList。

例如爲:

public class myListItem { 
    private String text; 
    private int id; 

public String toString() { 
    return text; 
} 
...getters/setters and constructor.... 

//Add to list: 
JList myList = new JList(); 
myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
DefaultListModel listModel = new DefaultListModel(); 
listModel.addElement(new MyListItem("one", 1); 
listModel.addElement(new MyListItem("two", 2); 
lst_scans.setModel(listModel); 
lst_scans.setSelectedIndex(0); 

//Obtain selection: 
MyListItem item = (MyListItem) myList.getSelectedValue();