[英]Need to add JCheckBox In JTable Dynamically
I have two JTable
. 我有两个JTable
。 There are certain number of records in one JTable
. 一个JTable
有一定数量的记录。
Once the first JTable
is loaded I want to load equal number of JCheckbox
to be created in the second table. 加载第一个JTable
,我想加载相等数量的JCheckbox
,以在第二个表中创建。
I have this scenario ... vl pass both the tables in method addCheckBox
. 我有这种情况... vl传递方法addCheckBox
两个表。
private void addCheckBox(JTable procTableSrc, JTable procTableCk){
CheckBoxRenderer checkBoxRenderer = new CheckBoxRenderer();
EachRowRenderer rowRenderer = new EachRowRenderer();
int rows = procTableSrc.getRowCount();
DefaultTableModel dm = (DefaultTableModel)procTableCk.getModel();
Object [] data = new Object[][]{{new Boolean(false)},{new Boolean(false)}};
for(int i=1; i <=rows; i++){
rowRenderer.add(i, checkBoxRenderer);
//model.addRow(new Object []{new Boolean(false)});
}
}
Please help me with a code in achieving that. 请通过代码帮助我实现这一目标。
Thank you for your edits, but you still might want to show us more and tell us what errors your current code is causing. 感谢您的修改,但您仍然可能想向我们展示更多信息,并告诉我们您当前代码引起的错误。
Regarding your "CheckBoxRenderer" class, you don't need this. 关于“ CheckBoxRenderer”类,您不需要它。 Please read the JTable tutorial which you can find here . 请阅读JTable教程,您可以在这里找到它。 There you will see that all you need to do is override your table model's getColumnClass
method to return Boolean.class for the column of interest for it to display checkboxes. 在那里,您将需要做的就是重写表模型的getColumnClass
方法,为感兴趣的列返回Boolean.class,以使其显示复选框。
Luck. 运气。
Edit 1 编辑1
Also what is with "row renderer", and what purpose does it serve? “行渲染器”又是什么,它有什么作用? To add information to your JTable, you must add rows to its model, and I don't see your code doing that. 要将信息添加到您的JTable中,您必须在其模型中添加行,而我看不到您的代码会这样做。 Have a look at the DefaultTableModel API where you'll see the addRow(...) method which may help you a great deal. 看看DefaultTableModel API,您将在其中看到addRow(...)方法,这可能会对您有很大帮助。 Either that or create a new DefaultTableModel object with your data arrays. 或者使用您的数据数组创建一个新的DefaultTableModel对象。 In fact, this is probably better since you can then override its getColumnClass() method to return Boolean for the column that needs to dislay check boxes. 实际上,这可能更好,因为您可以覆盖其getColumnClass()方法以为需要分配复选框的列返回布尔值。
Edit 2 编辑2
Also this won't compile since you're declaring it as a one dimensional array and initializing it as a 2-dimensional array.: 另外,由于您将其声明为一维数组并将其初始化为二维数组,因此无法编译。
Object [] data = new Object[][]
Consider doing the following: 考虑执行以下操作:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.