Results 1 to 9 of 9
Thread: Disable a checkbox in jtable
- 03-24-2011, 01:45 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 84
- Rep Power
- 0
Disable a checkbox in jtable
In my Jtable i have a column which is a boolean type. I need to disable some of the cells in this column based on a condition.
It would be gud if the color of those cell which are to be disabled is different.
In the tablemodel ,I have returned a boolean in the getColumnClass for the particular column and in the isCellEditable method retuen false for that particular column.By doing this I am not able to click on the checkbox,but I want these cells to look diffrent so that the user acn differentiate.
How it is possible?
Thanks,
Manshu
- 03-24-2011, 03:15 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
You would need to create a custom renderer. Copy the "BooleanRenderer" class from the JTable source code. You would then need to add a line of code like:
Java Code:setEnabled( table.isCellEditable(row, column) ? true : false );
- 03-24-2011, 04:11 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 84
- Rep Power
- 0
Thanks a lot for your reply.
I am not aware how to create a cellrenderer?
Can you please explain or elaborate on your point.
Thanks,
- 03-24-2011, 04:13 PM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables" where you will find a section that explains the concept of renderers.
- 03-24-2011, 04:25 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 84
- Rep Power
- 0
Thanks for help,I had just one more doubt,i have a tableListener on my table .
If I use a BooleancellRenderer for a particular column ,will the tableListener still work?
Thanks,
- 03-24-2011, 04:38 PM #6
Why don't you try it and see?
db
- 03-24-2011, 04:39 PM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
I don't know what a "tableListener" is.
Why don't you try it first? Then if you have a problem you can post a question.
- 03-24-2011, 06:07 PM #8
Member
- Join Date
- Feb 2011
- Posts
- 84
- Rep Power
- 0
thanks,
I tried creating a class but my checkbox is not disabled even when the cellEditable is false
Following is a sample code
public class TestBooleanRenderer extends JCheckBox implements TableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
setEnabled( table.isCellEditable(row, column) ? true : false );
return this;
}
}
and called it in view class as
table.getColumnModel().getColumn(10).setCellRender er(new TestBooleanRenderer () );
I put some print statements inside myBooleanRenedere i,it is not called.What mistake I am doing?
Thanks,
- 03-24-2011, 06:29 PM #9
To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
Use code tags to post codes -- [code]CODE[/code] will display asdbJava Code:CODE
Similar Threads
-
JTable disable drag and drop row
By NextEpisode1 in forum AWT / SwingReplies: 1Last Post: 02-10-2011, 06:46 PM -
Adding Checkbox to Jtable through vectors
By indra00 in forum AWT / SwingReplies: 2Last Post: 12-08-2010, 08:45 AM -
JTable: Disable sorting on one column
By b.newworld@gmail.com in forum AWT / SwingReplies: 1Last Post: 09-16-2010, 12:43 AM -
CheckBox in JTable
By nikosa in forum AWT / SwingReplies: 1Last Post: 08-05-2009, 05:01 AM -
checkBox in Jtable renderining problem
By pothraj in forum AWT / SwingReplies: 1Last Post: 12-18-2008, 11:39 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks