我有一个JTable,其中最后一列是Checkbox(multiple select)
。我想获取在特定按钮单击时选择的行的值。只需要检查的值。对此有何想法?
Object[] columnNamesTest = {"Request No","Assigned To","Copy Data","Updated Req Number"};
Object[][] rowDataTest = {
{"111111", "ABC",false,""},
{"222222", "XYZ", true,"999999"},
{"333333", "LMN",true,"444444"},
{"444444", "PQR", false,"555555"}
};
DefaultTableModel model = new DefaultTableModel(rowDataTest, columnNamesTest);
JTable table = new JTable(model){
private static final long serialVersionUID = 1L;
@Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return String.class;
case 1:
return String.class;
case 2:
return Boolean.class;
default:
return String.class;
}
}
@Override
public boolean isCellEditable(int row, int column) {
//Only the third column
return column == 2;
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(scrollPane);
答案 0 :(得分:1)
我想获取在特定按钮上单击所选行的值。只需要检查的值。
只需检查第三列的值,然后从DefaultTableModel
获取所需的值。
示例代码:
JButton button = new JButton("Show selcted records");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
for (int i = 0; i < model.getRowCount(); i++) {
Boolean value = (Boolean) model.getValueAt(i, 2);// check state
if (value) {
System.out.println(model.getValueAt(i, 1));// second column value
}
}
}
});