MainJFrame.java
/** * MainJFrame.java * * Created on Aug 2, 2013, 11:57:36 PM */ package me.dhanoop.forblog; import javax.swing.DefaultCellEditor; import javax.swing.JComboBox; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableCellEditor; /** * * @author dhanoopbhaskar */ public class MainJFrame extends javax.swing.JFrame { /** Creates new form MainJFrame */ public MainJFrame() { initComponents(); customizeTable(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") //DBTableModel.javaprivate void initComponents() { dbScrollPane = new javax.swing.JScrollPane(); dbTable = new javax.swing.JTable(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); dbTable.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { }, new String [] { "Title 1" } )); dbScrollPane.setViewportView(dbTable); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(dbScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(dbScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE) ); pack(); }// /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MainJFrame().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JScrollPane dbScrollPane; private javax.swing.JTable dbTable; // End of variables declaration private void customizeTable() { Object[] columnNames = new Object[1]; columnNames[0] = "ComboBox"; Object[][] rowData = new Object[1][1]; rowData[0][0] = "select gender"; DefaultTableModel tableModel = new DBTableModel(rowData, columnNames); dbTable.setModel(tableModel); String[] gender = {"Male", "Female"}; JComboBox tableData = new JComboBox(gender); dbTable.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(tableData)); dbTable.updateUI(); } }
package me.dhanoop.forblog; import javax.swing.table.DefaultTableModel; /** * * @author dhanoopbhaskar */ public class DBTableModel extends DefaultTableModel { public DBTableModel(Object[] columns) { super(columns, 0); } public DBTableModel(Object[][] data, Object[] columns) { super(data, columns); } @Override public boolean isCellEditable(int row, int column) { return true; } }
The following is the key snippet in the program. What we do here is simply changing the CellEditor of the ColumnModel of the column at position 0 (zero) of the table.
private void customizeTable() { Object[] columnNames = new Object[1]; columnNames[0] = "ComboBox"; Object[][] rowData = new Object[1][1]; rowData[0][0] = "select gender"; DefaultTableModel tableModel = new DBTableModel(rowData, columnNames); dbTable.setModel(tableModel); String[] gender = {"Male", "Female"}; JComboBox tableData = new JComboBox(gender); dbTable.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(tableData)); dbTable.updateUI(); }
Here we use the following constructor of the class DefaultCellEditor.
public javax.swing.DefaultCellEditor(javax.swing.JComboBox);
In the next post we shall discuss on handling of various events in such combo boxes which are added to the table.
0 Comments