开发者

Need to add JCheckBox In JTable Dynamically

开发者 https://www.devze.com 2023-03-26 16:41 出处:网络
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 thesecond table.

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 hel开发者_JAVA百科p 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. 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.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号