Results 1 to 9 of 9
  1. #1
    pink123 is offline Member
    Join Date
    Feb 2011
    Posts
    84
    Rep Power
    0

    Default 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

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    12

    Default

    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 );

  3. #3
    pink123 is offline Member
    Join Date
    Feb 2011
    Posts
    84
    Rep Power
    0

    Default

    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,

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    12

    Default

    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.

  5. #5
    pink123 is offline Member
    Join Date
    Feb 2011
    Posts
    84
    Rep Power
    0

    Default

    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,

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default

    Why don't you try it and see?

    db

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    12

    Default

    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.

  8. #8
    pink123 is offline Member
    Join Date
    Feb 2011
    Posts
    84
    Rep Power
    0

    Default

    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,

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default

    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 as
    Java Code:
    CODE
    db

Similar Threads

  1. JTable disable drag and drop row
    By NextEpisode1 in forum AWT / Swing
    Replies: 1
    Last Post: 02-10-2011, 07:46 PM
  2. Adding Checkbox to Jtable through vectors
    By indra00 in forum AWT / Swing
    Replies: 2
    Last Post: 12-08-2010, 09:45 AM
  3. JTable: Disable sorting on one column
    By b.newworld@gmail.com in forum AWT / Swing
    Replies: 1
    Last Post: 09-16-2010, 01:43 AM
  4. CheckBox in JTable
    By nikosa in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2009, 06:01 AM
  5. checkBox in Jtable renderining problem
    By pothraj in forum AWT / Swing
    Replies: 1
    Last Post: 12-18-2008, 12:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •