aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable And CheckBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable And CheckBox" Watch "JTable And CheckBox" New topic
Author

JTable And CheckBox

vinaya kumar
Ranch Hand

Joined: Jan 20, 2002
Posts: 70
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
Anuradha Saravanamuthu
Greenhorn

Joined: Jan 27, 2002
Posts: 5
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

Joined: Jan 20, 2002
Posts: 70
Thanks a lot
Clara Zetkin
Greenhorn

Joined: Dec 10, 2002
Posts: 19
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

Joined: Dec 10, 2002
Posts: 19
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.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Take a look at these examples. They should help you out.
Clara Zetkin
Greenhorn

Joined: Dec 10, 2002
Posts: 19
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

Joined: May 17, 2001
Posts: 2823
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

Joined: Dec 10, 2002
Posts: 19
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

Joined: May 17, 2001
Posts: 2823
It should. If not post your code.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JTable And CheckBox