How to implement JComboBox in JTable?

UPDATED: 28 May 2013
JTable and ComboBox
JTable and ComboBox
Its very tough for beginner to implement JComboBox with in JTable. I thought let me explain this example with all basic requirement one require. We're going to perform the below operations on JComboBox.

  • Implement JComboBox in JTable.
  • JComboBox value from Database.
  • Database index of the selected JComboBox.

So lets not waste time and take a look at the code. First of all you'll have to decide when the JTable update its model or say it's data. I used formWindowOpened event to load data in JTable.

private JComboBox comboBox;
//Above variable declared globally.
private void formWindowOpened(java.awt.event.WindowEvent evt) {
        Object[][] rowData = new Object[5][1];
        Object columnNames[] = {"Name of Item"};
        DefaultTableModel tm = new DefaultTableModel(rowData, columnNames);
        jTable1.setModel(tm);
        /*
         * Below method creates the JComboBox in JTable. Check the method code below.
         * jTable1.getColumnModel().getColumn(0) : set the column index as per your need.
         */
        setItemNameColumn(jTable1.getColumnModel().getColumn(0));
}
private void setItemNameColumn(TableColumn itemColumn) {
        comboBox = new JComboBox();
        try {
            Connection con = variables.getDatabaseConnection();
            PreparedStatement item_stmt = con.prepareStatement("select idwarehouse,NameOfProduct from warehouse;");
            ResultSet item_rs = item_stmt.executeQuery();
            while (item_rs.next()) {
                comboBox.addItem(new Item(item_rs.getInt("idwarehouse"), item_rs.getString("NameOfProduct")));
               // Bind Name of product and index in ComboBox
            }
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        comboBox.addActionListener(this); // Set action listener 
        itemColumn.setCellEditor(new DefaultCellEditor(comboBox));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setToolTipText("Click for combo box");
        itemColumn.setCellRenderer(renderer);
}

// Bean for ComboBox
public class Item {
        private int id;
        private String description;
        public Item(int id, String description) {
            this.id = id;
            this.description = description;
        }
        public int getId() {
            return id;
        }
        public String getDescription() {
            return description;
        }
        public String toString() {
            return description;
        }
}
// Action listner that read the selected index every time and shows database index.
@Override
public void actionPerformed(ActionEvent e) {
   if (e.getSource().equals(comboBox)) {
         comboBox = (JComboBox) e.getSource();
         Item item = (Item) comboBox.getSelectedItem();
         if(item!=null){
              System.out.println("Database Index:"+item.getId());
         }
   }
}
That's it. All you need to put all code in your project. If you have any problem comment here. 

0 comments :