如何从jtable中的组合框获取值和选定值?

时间:2012-07-24 02:20:51

标签: java swing combobox jtable jcombobox

在提出问题之前,我会尝试解释我需要和正在尝试做的事情。我已经设置了一个表格,其中有几列显示组合框,如图所示:

picture http://www.freeimagehosting.net/newuploads/4ks9s.png

这应该为这个工作创建一个“订单”,工作1将首先进入工作站1.如果我在工作站2列上添加工作站4,它将转到工作站4,依此类推。它旨在创建进一步处理的订单。所以,我想:

  • 创建表格并显示渲染的组合框;
  • 如果前一列的值为“none”,则使列(3-6)中的单元格不可编辑(从而确保其保持正确的顺序);
  • 不显示已为该行选择的电台;

但对于初学者来说,从组合框中选择时我无法获得正在设置的值,也无法获得这些值!

到目前为止,Heres是我的代码。

创建组合框:

public class SimGui extends JFrame {
                          //implements ActionListener {
    String Stations[] = new String[] {"Station 1","Station 2","Station 3","Station 4","Station 5","None"};
    JComboBox stationscombo = new JComboBox(Stations);
    Object obj = stationscombo.getSelectedItem();

桌面上的鼠标点击事件:

private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {                                     
    //Object event = evt.getSource();
    obj = stationscombo.getSelectedItem();
    System.out.println("Item: " + obj);
    //ListSelectionModel selectionModel = jTable2.getSelectionModel();
    int tb1columns = jTable2.getColumnCount();
    int selectionrow= jTable2.getSelectedRow();
    int selectioncolumn = jTable2.getSelectedColumn();
    if (selectioncolumn > 1) {
        for (int i=2;i<tb1columns;i++) {
            System.out.println(jTable2.getValueAt(selectionrow,selectioncolumn));
            /*if (jTable2.getValueAt(selectionrow, i) != "None") {
                stationscombo.removeItem(jTable2.getValueAt(selectionrow, i));
            }*/
        }
    }
    else { System.out.println(jTable2.getValueAt(selectionrow,selectioncolumn)); }
}

表格结构:

jTable2.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {
        {null, null, null, null, null, null, null},
        {null, null, null, null, null, null, null},
        {null, null, null, null, null, null, null}
    },
    new String [] {
        "Job Type", "Parts", "Station 1", "Station 2", "Station 3", "Station 4", "Station 5"
    }
) {
    Class[] types = new Class [] {
        java.lang.Integer.class, java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
    };
    boolean[] canEdit = new boolean [] {
        false, true, true, true, true, true, true
    };

    public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit [columnIndex];
    }
});
jTable2.getTableHeader().setReorderingAllowed(false);
for (int x = 2;x<7;x++) {
    jTable2.getColumnModel().getColumn(x).setCellEditor(new DefaultCellEditor(stationscombo));
}
jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        jTable2MouseClicked(evt);
    }
});

我环顾四周,试图为组合框实现一个监听器,但失败了。如果我在类上实现ActionListener,它将显示一条警告:

SimGui is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener

我已经为那些正在运行的表实现了一个ActionListener,但我不知道这是否会干扰?

Action action = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getSource());
        //TableCellListener tcl = (TableCellListener)e.getSource();
        //JComboBox cb = (JComboBox)e.getSource();
        //String newSelection = (String)cb.getSelectedItem();
        /*JComboBox cb = (JComboBox)e.getSource();
        String teste = (String)cb.getSelectedItem();
        System.out.println("Item: " + teste);*/
        //TableCellListener tcl1 = new TableCellListener(jTable1, action);
        /*if (tcl.getColumn() == 3) {
            if (tcl.getNewValue() == true) {
                int x = tcl.getColumn();
                table1model.setColumnEditable(x, true);
            }
            else { 
                table1model.setColumnEditable(tcl.getColumn(), false);
            }
            /*boolean canEdit[] = {
            false, true, true, true, true, true
            };
            //System.out.println(isCellEditable(tcl1.getRow(),tcl1.getColumn()));
        }*/
        /*System.out.println(newSelection);
        System.out.println("Row   : " + tcl.getRow());
        System.out.println("Column: " + tcl.getColumn());
        System.out.println("Old   : " + tcl.getOldValue());
        System.out.println("New   : " + tcl.getNewValue());*/
    }
};

但是底线,我的问题是,我怎样才能正确使用表格中的组合框,以便我可以获取设置的值并在选中时获取项目值?

1 个答案:

答案 0 :(得分:3)

查看TableModel.setColumnValue(row, col)

您可能也希望在table cell editors上阅读,这有助于您准确了解正在发生的事情。