Forums Register Login

JTable And CheckBox

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks a lot
+Pie Number of slices to send: Send
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.

 
+Pie Number of slices to send: Send
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.

 
+Pie Number of slices to send: Send
Take a look at these examples. They should help you out.
+Pie Number of slices to send: Send
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())); ?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
It should. If not post your code.
Happily living in the valley of the dried frogs with a few tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2543 times.
Similar Threads
JTable
JTable
JTable
JTable
JTable
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:46:35.