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

    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) {
             rendererComponent.setSelected(true);
          }
          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!!

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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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-09-2009, 11:25 PM
  2. Checkbox to list
    By msixty in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 08:10 PM
  3. checkBox in Jtable renderining problem
    By pothraj in forum AWT / Swing
    Replies: 1
    Last Post: 12-18-2008, 11:39 AM
  4. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  5. checkbox
    By Alan in forum AWT / Swing
    Replies: 3
    Last Post: 05-18-2007, 10: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
  •