gpt4 book ai didi

java - 单击按钮时选择复选框 JTable 的值

转载 作者:太空宇宙 更新时间:2023-11-04 06:38:09 26 4
gpt4 key购买 nike

我有一个 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);

最佳答案

I want to get the values of the rows selected on a particular button click.Only the values which are checked are needed.

只需检查第三列的值并从 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
}
}
}
});

关于java - 单击按钮时选择复选框 JTable 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25057782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com