• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable And CheckBox

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
i want to have a Jtable where in one column
i want to be checkbox.
i.e.
If i make the cell editor checkbox,
then only when i click ,i will
see the checkbox,i will see the value true or false.
so,now i want the checkbox to be displayed always
How do i do this!!!
Thanks and Regards
vinaa
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi vinay,
If u initialize ur table model with the boolean object value , by default jtable will put the combo box. The problem with this is it is selectable only with mouse. So, u can add custom checkbox editor and renderer to make the checkbox being selectable on tab key moment and selection on spacebar.
thanx & regards,
S.A.Radha.
 
vinaya kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I would be happy enought to have combo boxes instead of checkboxes. So what I'm doing is:
public class DBdetailTableModel extends AbstractTableModel {
....
public DBdetailTableModel(Connection connection)
{
this.connection = connection;
}
public void executePrepStmt(PreparedStatement prepStmt)
{
if (connection == null || prepStmt == null) {
System.err.println("There is no database to execute the query.");
return;
}
try {
resultSet = prepStmt.executeQuery();
metaData = resultSet.getMetaData();
int numberOfColumns = 6; columnNames = new String[numberOfColumns];
columnNames[0] = "Sample_Id";
.....
columnNames[5] = "Yes/NO
......
while(resultSet.next())
{
...
newRow[5]= new Boolean(false);

}

Originally posted by Anuradha Saravanamuthu:
Hi vinay,
If u initialize ur table model with the boolean object value , by default jtable will put the combo box. The problem with this is it is selectable only with mouse. So, u can add custom checkbox editor and renderer to make the checkbox being selectable on tab key moment and selection on spacebar.
thanx & regards,
S.A.Radha.

 
Clara Zetkin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I would be happy enought to have combo boxes instead of checkboxes. So what I'm doing is:
public class DBdetailTableModel extends AbstractTableModel {
....
public DBdetailTableModel(Connection connection)
{
this.connection = connection;
}
public void executePrepStmt(PreparedStatement prepStmt)
{
if (connection == null || prepStmt == null) {
System.err.println("There is no database to execute the query.");
return;
}
try {
resultSet = prepStmt.executeQuery();
metaData = resultSet.getMetaData();
int numberOfColumns = 6; columnNames = new String[numberOfColumns];
columnNames[0] = "Sample_Id";
.....
columnNames[5] = "Yes/NO
......
while(resultSet.next())
{
...
newRow[5]= new Boolean(false); .......
public boolean isCellEditable(int row, int column)
{
if (column == 5)
return true;
else
return false;
}
}
And in the GUI class:
dt = new DBdetailTableModel(connection);
jTable1 = new JTable(dt);
PreparedStatement prepStm = dt.connection.prepareStatement(query);
dt.executePrepStmt(prepStm);
jTable1.getColumnModel().getColumn(5).setCellEditor(new DefaultCellEditor (new JCheckBox()));
But I see the checkBox only when I'm trying to edit the cell, otherwise it says just "false".
Could you help me?

Originally posted by Anuradha Saravanamuthu:
Hi vinay,
If u initialize ur table model with the boolean object value , by default jtable will put the combo box. The problem with this is it is selectable only with mouse. So, u can add custom checkbox editor and renderer to make the checkbox being selectable on tab key moment and selection on spacebar.
thanx & regards,
S.A.Radha.

 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at these examples. They should help you out.
 
Clara Zetkin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, but I know these examples, they are far to complicated for what I want to do... I mean, I hoped there where simpler ways.. A table with just one column checkboxes? Oh, there are times I wish for Visual Basic! (wrong thing to say here, suppose..)
So there is no one-liner like:
jTable1.getColumnModel().getColumn(5).setCellEditor(new DefaultCellEditor (new JCheckBox())); ?
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are placing Boolean objects in the table it should already be ok. Are you putting Boolean or boolean (true/false)? It makes a difference.
 
Clara Zetkin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the difference? I'm using "new Boolean(false)" at the moment.
In the meantime, I worked out that the main problem is, that my rows are Vectors, but the colums are Arrays. So I hope it will work when I fixed that...
Thanks!
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should. If not post your code.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic