• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Adding checkbox in table column

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ,
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ..
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey .. I got the code.. It's working fine now.. ... Posting the code for reference ...

 
Danger, 10,000 volts, very electic .... tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic