Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default Adding JCheckBox to JTable

    Hi All,

    I am trying to add a JCheckBox to my JTable, but when ran the check boxes are showing up as "javax.swing..".

    Please help.

    Thanks,

    Java Code:
    private void makeScrollPane(ResultSet r) {
                   DefaultTableModel model = new DefaultTableModel();
                   JTable table = new JTable(model);
                   if (pane !=null) remove(pane);
                   try {
                           ResultSetMetaData m = r.getMetaData();
                           int colCount = m.getColumnCount();
    
                           for (int i = 1;  i <= colCount; i++) {
                                   model.addColumn(m.getColumnName(i));
                           }
                          [B] JCheckBox row2[] = new JCheckBox[colCount];
                           for(int j = 1; j <= colCount; j++){
                               row2[j-1] = new JCheckBox();
                           }
                           model.addRow(row2);[/B]
                                                                                          
                           while (r.next()) {
                                   String row1[] = new String[colCount];
                                   for (int i = 1; i <= colCount; i++) {
                                           row1[i-1] = r.getString(i);
                                  }
                                   model.addRow(row1);       
                                 
                           } 
                   
                   }
                   catch (SQLException e) {}
                   pane = new JScrollPane(table);
    
                   pane.setBounds(200, 5, 300, 600);
    
                   add(pane);
                   repaint();
    
           }

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

    Default

    Are you trying to add a row of JCheckBox or a column? The former doesn't make sense to me, and the latter is well explained in the Swing JTable tutorial, and mainly consists of overloading the model's getColumnClass method to return Boolean.class. Please have a look at the tutorial for the details.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    And as you've already discovered, adding Swing components to a model doesn't automagically result n those components being used to render/edit a JTable or any other Swing component that uses renderers and editors.

    db

Similar Threads

  1. JCheckbox in jtable
    By anilkumar_vist in forum Advanced Java
    Replies: 3
    Last Post: 09-07-2010, 06:29 AM
  2. adding integers in JTable
    By khanzaman in forum AWT / Swing
    Replies: 2
    Last Post: 06-14-2010, 10:12 PM
  3. JCheckbox in a Jtable Column
    By kedia_rohit in forum AWT / Swing
    Replies: 1
    Last Post: 04-25-2010, 01:45 PM
  4. How to add a jcheckbox in jtable
    By lakshayghai in forum AWT / Swing
    Replies: 9
    Last Post: 04-15-2010, 12:32 AM
  5. JCheckBox in JTable column
    By hind in forum New To Java
    Replies: 8
    Last Post: 01-04-2009, 08:40 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
  •