【问题标题】:JComboBox submit value like HTML selectJComboBox 提交值,如 HTML 选择
【发布时间】:2010-08-10 05:31:41
【问题描述】:

我有一个包含两列的表格 - iddesc。是否可以在 JComboBox 中向用户显示desc 列值的列表,当用户从列表中选择一个项目并单击一个按钮时,我会得到其对应的id 值?

也就是说,在 Swing 中可以做这样的事情吗?

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

当用户点击 Hello 时,我需要 2 作为值。

【问题讨论】:

    标签: java swing


    【解决方案1】:

    您可以将任何对象传递给您的 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();
    

    【讨论】:

      【解决方案2】:

      这是一个使用 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 来存储数据,然后从 TableColumnModel 中删除第一个 TableColumn,使其在表中不可见。

      【讨论】:

      • 哇... camickr !刚刚注意到..!!
      猜你喜欢
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      相关资源
      最近更新 更多