我在 JTable
中使用复选框,其本身是 JPanel
的一部分。最初我使用的是 JOptionPane,单击“确定”按钮后我获得了值,但现在我在 JFrame 中添加了 JPanel。当我单击右上角的 X
符号时,它不会检索单击的复选框的值,但能够获取其他列的值。代码片段如下:
DefaultTableModel dtm = new DefaultTableModel(rowData, columnNames)
{
};
for (int i = 0; i < records.size(); i++)
{
// System.out.println(records.get(i));
singleRecord = records.get(i).toString().split("%");
Pages = singleRecord[0].toString();
BKey= singleRecord[1].toString();
Title = singleRecord[2].toString();
Author = singleRecord[3].toString();
TimeStamp = singleRecord[4].toString();
dtm.addRow(new Object[] { Boolean.FALSE ,Pages,BKey,Title,Author,TimeStamp});
}
table = new javax.swing.JTable(dtm)
{
public boolean isCellEditable(int row,int column)
{
/*if(column == 0)
return true;
else
return false;
*
*/
return(column < 2);
}
};
for (int i = 0; i < table.getRowCount(); i++)
{
System.out.println(table.getValueAt(i,1).toString());
boolean isChecked = (Boolean) table.getValueAt(i,0);//always return false
if (isChecked)
{
System.out.println("checked ");
Ids+=table.getValueAt(i,2).toString()+"%";
}
}
最佳答案
遵循 Java 变量命名标准。变量名称不应以大写字符开头。你们中有些人的名字是正确的。其他人则不然。保持一致!!!
如果您总是得到 false,那么可能是因为当您单击单元格时编辑器没有将值更新为 Boolean.TRUE。您不仅需要重写
isCellEditable(...)
方法来确定哪些单元格可编辑,还需要重写getColumnClass(...)
方法来确定哪些单元格可编辑。返回单元格的类(在本例中为Boolean.class
),以便表格可以使用适当的渲染器和编辑器。
阅读 Swing 教程中关于 How to Use Tables 的部分了解更多信息和工作示例。
关于java - JTable 中的复选框始终返回 false,尽管已选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24844884/