jtable 上的复选框并获取 java swing 中相应行的值
我在 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));
在 netbeans 中一样。
我使用 addListSelectionListener 单击 JTable 的 JCheckBox 的单元格。
jTable1.getSelectionModel().addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(ListSelectionEvent event ) {
// if(jTable1.getValueAt(0,7).equals(true)){
Object b=jTable1.getValueAt(0,7);
System.out.println(b);
//}
}
});
我不明白为什么它第一次打印 null,然后它会打印 value 2 次。
I add checkbox in JTable.I want to get values of particular cells of the selected JCheckBox rows.adding checkbox code is like
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));
in netbeans.
I use addListSelectionListener for clicking on cells of JCheckBox of JTable.
jTable1.getSelectionModel().addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(ListSelectionEvent event ) {
// if(jTable1.getValueAt(0,7).equals(true)){
Object b=jTable1.getValueAt(0,7);
System.out.println(b);
//}
}
});
I donot understand why it is print null at first time and after that it will print value 2 times.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无需创建和分配自定义编辑器。 JTable 将根据
getColumnClass(,..)
方法返回的类返回适当的渲染器和编辑器。复选框会自动用于布尔数据。ListSelectionListener 会触发两个事件,一个用于取消选择先前选定的行,另一个用于选择当前行。
There is no need to create and assign a custom editor. JTable will return the appropriate renderer and editor based on the class returned from the
getColumnClass(,..)
method. A checkbox is automatically used forBoolean
data.A ListSelectionListener fires two events, one for the deselection of the previously selected row and one for selecting the current row.
在单击任何单元格之前,该字段中的值为空(我没有看到您在示例代码中为表格设置任何内容)
在选中单元格中的复选框之后,它现在将具有由复选框设置的显式真值。
再次单击它后,它将具有由复选框设置的显式设置的空值。
before clicking any cells, the value in that field is null (i don't see you setting any content to your table in the example code)
after checking a checkbox in cell, it will now have an explicit true value as set by the checkbox.
after clicking it again, it will have an explicitly set null value, set by the checkbox.