aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Adding checkbox in table column Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding checkbox in table column" Watch "Adding checkbox in table column" New topic
Author

Adding checkbox in table column

Rite Sara
Ranch Hand

Joined: Feb 13, 2010
Posts: 56
Hi,

I want to add checkbox in a table column. I used following code for this.


I am able to get checkboxes in a column....but the issue is ... they are not selectable i.e. I am not able to tick them...
Also, I would like to know how we can get row value for which checkbox is checked ??



Thanks ,
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
try the sample code from the sun site 'how to use tables',
one of the columns is a (selectable/tickable) checkbox
Rite Sara
Ranch Hand

Joined: Feb 13, 2010
Posts: 56
Hi,

I had a look at following code given by SUN ...
http://java.sun.com/docs/books/tutorial/uiswing/examples/components/TableSelectionDemoProject/src/components/TableSelectionDemo.java

But since I am very new to Swings, was unable to modify that code as per my requirement .. tried a lott ...
My requirement is very simple.. There are 2 columns in a table .. 1st -> Checkbox ... 2nd -> Name .. and there is one button (OK)..
Onclick of button OK ... I want a list which contains values (Names) for which checkbox is checked ...
It's very basic requirement ... It would be really helpful if someone provide sample code for this ..
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
I want a list which contains values (Names) for which checkbox is checked ...


1) Create a loop
2) use the model.getValueAt(...) method to get the value from the column containing the Boolean value
3) if ( theValue.equals( Boolean.TRUE ) ) then get the Name from the model using the getValueAt() method for the current row and specific column.
Rite Sara
Ranch Hand

Joined: Feb 13, 2010
Posts: 56
I am using the code similar to code posted in previous post and I am getting checkboxes in a table .. but not able to check(click) them ...
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
I am using the code similar to code posted in previous post


I don't see your SSCCE posted anywhere that shows the similiar code. The key is to override the getColumnClass() method of the JTable or the model to return the proper class so the table can choose the proper renderer and editor to use.
Rite Sara
Ranch Hand

Joined: Feb 13, 2010
Posts: 56
I don't see your SSCCE posted anywhere that shows the similiar code. .


Here I am posting my code again ...



I am overriding getColumnClass() here.. and I am getting checkboxes in the column ...
But the issue is I am not able to check them .
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
How is your code like the demo code from the tutorial?

Where in the demo code:

a) is a custom renderer used?
b) is a JCheckBox added to the model?

Start with a working example and make changes to the working code 1 at a time. Then if is stops working you know what you changed to cause the problem.
Rite Sara
Ranch Hand

Joined: Feb 13, 2010
Posts: 56
This is working example only ... Yes..its from tutorial ...but I have implemeted same way in my code ...
Given below are the steps showing the way I am implementing ...

1st ...
I am making object array with checkboxes .. and adding it to model ...

Object[][] data =
{
{new javax.swing.JCheckBox() , "test"},
};

DefaultTableModel model = new DefaultTableModel(data, columnNames);

2nd..
Implementing getColumnClass()

JTable table = new JTable( model )
{
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
};

3rd..
Implementing table cell renderer

class MyCellRenderer implements TableCellRenderer
{
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)
{
Component com = (Component)value;
return com;
}

}

4th..

fuction call to
table.setDefaultRenderer(Component.class, new MyCellRenderer());

As I said earlier...I am getting checkboxes in the column ...
But the issue is I am not able to check or select them ...
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
The tutorial does NOT:

a) add a JCheckBox to the model (look at the code to see what they use)
b) use a custom renderer (get rid of this code)

Rite Sara
Ranch Hand

Joined: Feb 13, 2010
Posts: 56
Hey .. I got the code.. It's working fine now.. ... Posting the code for reference ...

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding checkbox in table column