Need to add JCheckBox In JTable Dynamically

陌路散爱 提交于 2019-12-24 02:31:18

问题


I have two JTable. There are certain number of records in one JTable.

Once the first JTable is loaded I want to load equal number of JCheckbox to be created in the second table.

I have this scenario ... vl pass both the tables in method 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.


回答1:


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. Please read the JTable tutorial which you can find here. 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.

Luck.

Edit 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. Have a look at the DefaultTableModel API where you'll see the addRow(...) method which may help you a great deal. Either that or create a new DefaultTableModel object with your data arrays. 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.

Edit 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.
  • 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.
  • 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. 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.


来源:https://stackoverflow.com/questions/6972182/need-to-add-jcheckbox-in-jtable-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!