Saturday, August 3, 2013

Java - JComboBox in a JTable cell (with event handling)

 
Please refer to the previous post here...
The function customizeTable() has been modified in order to include event handling functionality in the combo box. 
 
    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"};
        tableData = new JComboBox(gender);
        tableData.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String item = (String) tableData.getSelectedItem();
                JOptionPane.showMessageDialog(new JFrame(), item, "Selected Item", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        dbTable.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(tableData));
        dbTable.updateUI();
    }

For the JComboBox tableData to be accessible in the inner class' method actionPerformed() it should be declared in class level as below. (In the previous post, it's declared locally).
 
public class MainJFrame extends javax.swing.JFrame {
    private JComboBox tableData = null;
.
.
.






0 comments:

Post a Comment

Contact Form

Name

Email *

Message *