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);
        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));

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;


Post a Comment

Contact Form


Email *

Message *

The Insane Techie - Android App

Launched an android app for the blog on 07th June 2016. Get it from google play store... Tips for using the app Use in landscape mo...