简体   繁体   English

需要在JTable中动态添加JCheckBox

[英]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: 考虑执行以下操作:

  • Create a 2-dimensional array of Object and have it hold your model's data. 创建一个二维对象数组,并保存模型的数据。 The array's first index will be the the number of rows displayed in the JTable and the second will be the number of columns. 数组的第一个索引是JTable中显示的行数,第二个索引是列数。
  • Fill each column position with your Booleans. 用您的布尔值填充每个列位置。
  • Create a new DefaultTableModel object, one that overrides getColumnClass(...) and has it return Boolean.class for the column that holds Booleans and needs to display check boxes. 创建一个新的DefaultTableModel对象,该对象将覆盖getColumnClass(...),并使该对象为包含布尔值并需要显示复选框的列返回Boolean.class。
  • Give it a constructor that allows you to pass in the 2-D Object array and perhaps an array of String for the column headers. 给它一个构造函数,使您可以传入2-D对象数组,也可以为列标题传递一个String数组。 The first line of the constructor should be a call to the super constructor and you will need to pass the array parameters into this call. 构造函数的第一行应该是对超级构造函数的调用,您将需要将数组参数传递给此调用。
  • call setModel on your procTableCk object passing in this model that you've just created. 在procTableCk对象上调用setModel,传入刚刚创建的模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM