checkbox in jtable cell


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: checkbox in jtable cell

Hybrid View

  1. #1
    Join Date
    Mar 2005
    Posts
    19

    checkbox in jtable cell

    Hi,

    I'm trying to get a jtable with two columns in it. The first column is just boolean values which I'd like to display as checkboxes and the second column contains strings (representing table names that the user can select using the checkbox).

    So far I have managed to get the table populated with the data, but I don't know how to render the cells in the first column to be checkboxes.

    I also need to get the values (from the 2nd column) of all the selected items.

    note that there's also a combo box being populated with the same data here as well.

    Code:
    	private void populateTables() {
    	    
    	    cmbStagingTable.removeAllItems();
    	    
    	    DefaultTableModel model  = new DefaultTableModel();
    	    model.addColumn("Include");
    	    model.addColumn("Table");
    	    
     	    List tables = core.dt.getAllTables(core.dbInfo);
    	    for (int x = 0; x < tables.size(); x++) {
    	        cmbStagingTable.addItem(tables.get(x));
     
    	        Vector row = new Vector();
    	        row.add(Integer.toString(0));
    	        row.add(tables.get(x));
    	        model.addRow(row);
    	        
    	    }
     
    	    tblDestTables.setModel(model);
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Here are the bare bones...

    ..just using the JTable default behaviour, the clue is to override the

    public Class getColumnClass(int column) method.

    Code:
    class ATableClass extends JTable {
      public Class getColumnClass(int column) {
        try {
          if (column == 0) {
            return Class.forName("java.lang.Boolean");
          }
          return Class.forName("java.lang.Object");
        }
        catch (ClassNotFoundException ex) {
          ex.printStackTrace();
          return null;
        }
      }
    }
    public class TableExample1 extends JFrame {
      JScrollPane jScrollPane1 = new JScrollPane();
      ATableClass tableOne = new ATableClass();
      DefaultTableModel model  = new DefaultTableModel();
     
      Boolean [] boolArray=new Boolean[] {
          new Boolean(true),
          new Boolean(false),
          new Boolean(true),
          new Boolean(false),
          new Boolean(true),
          new Boolean(false)
      };
    
      public TableExample1() {
        try {
          jbInit();
          tableOne.setModel(model);
          model.addColumn("CbxColumn", boolArray);
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public static void main(String[] args) {
        TableExample1 tex = new TableExample1();
        tex.setBounds(10,10,300,200);
        tex.setVisible(true);
      }
      private void jbInit() throws Exception {
        this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
        jScrollPane1.getViewport().add(tableOne, null);
      }
    }
    Last edited by sjalle; 03-18-2005 at 08:35 AM.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center