请问如何在java swing table单元格增加个下拉框呢。
又如何获得这个下拉框的值呢?
麻烦大侠给出个代码。谢谢。

解决方案 »

  1.   


             Vector item = new Vector();
             item.add("1");
             item.add("2");
             item.add("3");
             item.add("4");
             item.add("5");
             item.add("5");
             JComboBox JComboBoxItem = new JComboBox(item);
             TableColumn  brandColumn = table.getColumnModel().getColumn(1);
             brandColumn.setCellEditor(new DefaultCellEditor(JComboBoxItem));
      

  2.   

    用JComboBox()
    String str = {"a","b","c"};
    JComboBox jcb = new JComboBox(str); 
      

  3.   

    好人做到底......
    import java.util.Vector;
    import javax.swing.DefaultCellEditor;
    import javax.swing.JComboBox;
    import javax.swing.table.TableColumn;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    public class TableFrame extends JFrame{
    public TableFrame(){
    super();

    JTable table = new JTable();
    table.setModel(new javax.swing.table.DefaultTableModel(
                 new Object [][] {
                      {null, null, null, null},
                     {null, null, null, null},
                     {null, null, null, null},
                     {null, null, null, null}
                },
                 new String [] {
                     "Title 1", "Title 2", "Title 3", "Title 4"
                }
            ));

    Vector item = new Vector();
        item.add("1");
        item.add("2");
        item.add("3");
        item.add("4");
        item.add("5");
        item.add("5");
        JComboBox jComboBoxItem = new JComboBox(item);
        jComboBoxItem.addItemListener(new   ItemListener(){
                public   void   itemStateChanged(ItemEvent   ie)   {
    System.out.println(ie.getItem().toString());
                }
        });
        
        TableColumn  tableColumn = table.getColumnModel().getColumn(1);
        tableColumn.setCellEditor(new DefaultCellEditor(jComboBoxItem));

    JScrollPane tablePanel = new JScrollPane(table);
    add(tablePanel);
    pack();
    setVisible(true);
    }
    public static void main(String[] args){
    TableFrame tableFrame = new TableFrame();
    }
    }