如果选中jtable中相应复选框的复选框,如何获取其他单元格的值

时间:2011-08-04 17:53:50

标签: java swing jtable

如果在JTable中选择了相应的JCheckBox复选框,如何获取其他单元格的值。我在JTable中添加了JCheckbox

JCheckBox checkBox = new javax.swing.JCheckBox();
jTable1 = new javax.swing.JTable();

jTable1.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {    
    },
    new String [] {
        "Station", "OperationName", "TliScantime", "StartTime", "Completedtime",
        "TliScanTime-StartTime", "StartTime-CompletedTime", "Select"
    }
) {
    Class[] types = new Class [] {
        java.lang.String.class, java.lang.String.class, java.lang.String.class,
        java.lang.String.class, java.lang.String.class, java.lang.String.class,
        java.lang.String.class, java.lang.Boolean.class
    };

    public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
    }
});
jTable1.getColumn("Select").setCellEditor(new DefaultCellEditor(checkBox));
jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
jTable1.getColumnModel().getColumn(0).setPreferredWidth(120);
jTable1.getColumnModel().getColumn(1).setPreferredWidth(100);
jTable1.getColumnModel().getColumn(2).setPreferredWidth(80);
jTable1.getColumnModel().getColumn(3).setPreferredWidth(100);
jTable1.getColumnModel().getColumn(4).setPreferredWidth(100);
jTable1.getColumnModel().getColumn(5).setPreferredWidth(180);
jTable1.getColumnModel().getColumn(6).setPreferredWidth(180);

jScrollPane1.setViewportView(jTable1);

现在我想在JCheckbox上点击它时,它会从TliScanTime-StartTime获取值,StartTime-CompletedTime所选行的列表示检查JCheckbox时。它将显示两个分隔的JLable中TliScanTime-StartTimeStartTime-CompletedTime的平均时间。并且值将动态更改意味着每次单击复选框时平均时间都会更改。我会在每行上添加复选框。

请提供一些编码示例的帮助。提前谢谢

2 个答案:

答案 0 :(得分:1)

您可以调整here显示的方法。

答案 1 :(得分:1)

对于JTable,您必须实施TableModelListener

例如

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TableCheckboxListenerExample {

    public static void main(String[] args) {
        try {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
            DefaultTableModel model = new DefaultTableModel(new Object[][]{
                        {Boolean.FALSE, "Row 1"},
                        {Boolean.TRUE, "Row 2"}},
                    new String[]{"col1", "col2"}) {

                private static final long serialVersionUID = 1L;

                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    if (getRowCount() > 0 && getValueAt(0, columnIndex) != null) {
                        return getValueAt(0, columnIndex).getClass();
                    }
                    return super.getColumnClass(columnIndex);
                }
            };
            model.addTableModelListener(new TableModelListener() {

                @Override
                public void tableChanged(TableModelEvent e) {
                    int row = e.getFirstRow();
                    int column = e.getColumn();
                    TableModel model = (TableModel) e.getSource();
                    Object data = model.getValueAt(row, column);
                    if (data instanceof Boolean) {
                        System.out.println("Value changed in Row: " + row + " Column: " + column + " New Value = " + data);
                    }
                }
            });
            JTable table = new JTable(model);
            frame.add(new JScrollPane(table));
            frame.pack();
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private TableCheckboxListenerExample() {
    }
}