【问题标题】:How to get values and selected values from combobox inside jtable?如何从 jtable 内的组合框中获取值和选定值?
【发布时间】:2012-07-24 02:20:50
【问题描述】:

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

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

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

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

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

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

创建组合框:

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());*/
    }
};

但归根结底,我的问题是,如何正确使用表格内的组合框,以便在选择时获取设置的值并获取项目值?

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java swing combobox jtable jcombobox


【解决方案1】:

看看TableModel.setColumnValue(row, col)

您可能还想在table cell editors 上做一些阅读,这将帮助您准确了解正在发生的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多