Results 1 to 2 of 2
  1. #1
    nikosa is offline Member
    Join Date
    Nov 2008
    Rep Power

    Default CheckBox in JTable

    Hello guys, I have a jtable and I want to add check box in the first column so that I can give the user the option to select some of the rows he wants.

    in the table I add this code to render the cell from the String value to boolean value but when I want to select some of the rows it displays back the string value.

    the code is
    Java Code:
    table.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() {
      // the method gives the component  like whome the cell must be rendered
       public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,boolean isFocused, int row, int col) {
          boolean marked = (Boolean) value;
          JCheckBox rendererComponent = new JCheckBox();
          if (marked) {
          return rendererComponent;
    can you help me in what to change so that I can do what I want. I am newbie in programming. I know that I have to create an array of checkboxes but don't know how to integrate them in this code. any help will be much appreciated!!

    Last edited by Fubarable; 08-05-2009 at 05:58 AM. Reason: code tags added to retain formatting

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    Don't set the cell renderer to do this but instead use a custom table model (I usually extend the DefaultTableModel) and in your getColumnClass override method return Boolean.class for the columns where you want a check box. For details on this and sample code, please see the Sun JTable tutorial: Sun Java JTable Tutorial

    Oh, I added code tags to your posted code above to improve readability. If you'd like to do this in the future, just highlight your code and press the "CODE" button.

    Best of luck and hope this helps.

Similar Threads

  1. Jtree with checkbox
    By msankar.ravi in forum AWT / Swing
    Replies: 1
    Last Post: 07-10-2009, 12:25 AM
  2. Checkbox to list
    By msixty in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 09:10 PM
  3. checkBox in Jtable renderining problem
    By pothraj in forum AWT / Swing
    Replies: 1
    Last Post: 12-18-2008, 12:39 PM
  4. Replies: 2
    Last Post: 05-15-2008, 11:15 AM
  5. checkbox
    By Alan in forum AWT / Swing
    Replies: 3
    Last Post: 05-18-2007, 11:10 AM

Posting Permissions

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