Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add a column of checkboxes to a JTable

Status
Not open for further replies.
May 13, 2002
75
GB
Hi,

I'm trying to add a column of checkboxes as the first column in a JTable, i tried
Code:
myJTable.addColumn(new TableColumn(1,10,new DefaultTableCellRenderer() ,null));

for (int i = 0; i < myJTable.getModel().getRowCount(); i++) 
      {
        myJTable.getModel().setValueAt(new Boolean(false),i,1);
      }

but just get a column of &quot;false&quot;, any ideas ?

Alistair [monkey]
 
Try this renderer instead of the DefaultTableCellRenderer :

public class MyCheckBoxRenderer extends JCheckBox implements TableCellRenderer {

public Component getTableCellRendererComponent ( JTable table,
Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
if ( value instanceof Boolean ) {
Boolean b = (Boolean)value;
setSelected( b.booleanValue() );
}
return this;
}

}
 
Ah, yes thanks, this works.

Is there a better way to do this ? If i want to allow the user to select the check boxes and retrieve the result, do i also have to create a MyCheckBoxEditor class implementing all methods from TableCellEditor and CellEditor ?

Thanks for your help


Alistair [monkey]
 
Specify the following as the editor :

new DefaultCellEditor(new JCheckBox())
 
Another way to do it is by overriding the getColumnClass(...) in the TableModel and let it return Boolean.Class for the column with the Boolean value
Code:
  public class MyDefaultTableModel extends DefaultTableModel {
    /* Supply the constructor that you need */
    public MyDefaultTableModel(Object[][] data, Object[] header) {
      super(data, header);
    }

    public Class getColumnClass(int col) {
      if (col == 3)
        return Boolean.class;
      ...
      return Object.class;
    }

  }
 
Ok, i've tried the following and it displays my table with an extra column with some checkboxes and it allows me to click them, however, when you do click them i get an error:

Exception occurred during event dispatching:
java.lang.ClassCastException: java.lang.Boolean

Is this because it's having trouble setting values in the tablemodel ? Here's some code...

Code:
//get some data i have
DefaultTableModel model = new DefaultTableModel((Object[][])o[1],(Object[])o[0]);
//create a new JTable from my model
JTable myTable = new JTable(model);

//create a new TableColumn to handle Boolean data in a table
TableColumn tc =new TableColumn(3,2,myTable.getDefaultRenderer(Boolean.class),myTable.getDefaultEditor(Boolean.class));
tc.setHeaderValue(new String(&quot;yes or no&quot;));
myTable.setAutoCreateColumnsFromModel(false);
    
//create an extra column in my model
model.addColumn(&quot;extra data&quot;);

//add TableColumn to table
myTable.addColumn(tc);

Thanks




Alistair [monkey]
 
How many colums does your table have before you add the extra column? Since you are creating a &quot;new TableColumn(...)&quot; with modelIndex=3 your table should have 3 colums before adding the extra column.
 
my tablemodel has 3 columns initially as does the JTable, but then i add a column to the model before i add a TableColumn to the JTable

//create an extra column in my model
//this is index 3
model.addColumn(&quot;extra data&quot;);

//add TableColumn to table
//this handles data in column index 3
myTable.addColumn(tc);

Alistair [monkey]
 
This program works for me without problems :
(Initially a table with 6 rows and 3 columns is created).

Code:
package com.ecc.swing2;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class Test2 extends JFrame {

  JTable myTable ;
  Object[][] data=new Object[6][3];
  Object header[]= {&quot;Jan&quot;,&quot;Feb&quot;,&quot;Mar&quot;};

  public Test2() {
    super();
      try {
        jbInit();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

  private void jbInit() throws Exception {
    for (int i=0;i<6;i++)
      for (int j=0;j<3;j++)
         data[i][j]=new Integer(i*10+j);
    myTable=new JTable(data,header);
    DefaultTableModel model = new DefaultTableModel(data,header);
    myTable.setModel(model);
    myTable.setCellSelectionEnabled(true);
    //create a new TableColumn to handle Boolean data in a table
    TableColumn tc =new TableColumn(3,4,myTable.getDefaultRenderer(Boolean.class),myTable.getDefaultEditor(Boolean.class));
    tc.setHeaderValue(new String(&quot;yes or no&quot;));
    myTable.setAutoCreateColumnsFromModel(false);
    //create an extra column in my model
    model.addColumn(&quot;extra data&quot;);
    //add TableColumn to table
    myTable.addColumn(tc);
    this.setTitle(&quot;Alistair&quot;);
    this.setSize(new Dimension(600, 300));
    this.getContentPane().add(new JScrollPane(myTable), BorderLayout.CENTER);
   }

  public static void main(String args[]) {
    Test2 myframe=new Test2();
    myframe.setVisible(true);
  }

}

But if you create your own MyDefaultTableModel (to override the getColumnClass() method, like I said in previous post, you can create the TableColumn by the following line of code (without specifying a renderer or editor).
Code:
TableColumn tc =new TableColumn(3,4);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top