Search code examples
javaswingjtablejcombobox

JTable, JComboBox - problems in showing JComboBox in second column


I have written this simple program:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class JcomboboxJtableDemo  extends JPanel
                          implements ActionListener {

    private DefaultTableModel tableModel;
    JTable table = new JTable (tableModel);
    private JScrollPane scrollpaneTable = new JScrollPane( table );
    private JPanel PaneBottoniTabella = new JPanel( );

    public JcomboboxJtableDemo() {
        super(new BorderLayout());

        String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };

        JComboBox comboBox = new JComboBox(petStrings);
        comboBox.setSelectedIndex(4);

        tableModel = CreateTableModel();

        tableModel.insertRow( 0, new Object[] {"Header col1", ""} );
        tableModel.insertRow( 0, new Object[] {petStrings[0], ""} );
        tableModel.insertRow( 0, new Object[] {petStrings[1], ""} );
        tableModel.insertRow( 0, new Object[] {petStrings[2], ""} );
        tableModel.insertRow( 0, new Object[] {petStrings[3], ""} );
        tableModel.setValueAt("Header col2", 0, 1); 

        DefaultCellEditor editor = new DefaultCellEditor(comboBox);
        table.getColumnModel().getColumn(0).setCellEditor(editor);
        table.getColumnModel().getColumn(1).setCellEditor(editor);



        //Lay out the demo.
        add(comboBox, BorderLayout.PAGE_START);
        add(table, BorderLayout.PAGE_END);
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    }


    private final DefaultTableModel CreateTableModel () {
     DefaultTableModel modello = new DefaultTableModel( new Object[] { "Col1","Col2" }, 0 ) {
        @Override
        public boolean isCellEditable(int row, int column) {
          return true;
        }
     };  
    table.setModel(modello);
        return modello;
   } 

   private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("ComboBoxDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new JcomboboxJtableDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
   public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

I you try to run it you will see that there is a problem in showing correctly the JComboBox components in the second column, in the first column the are correctly shown and you can see each selected item as set in the code, instead in the second column there are some problems: none on the relative cell.

Could you tell me why? How can I solve the problem?

Thanks


Solution

  • You're using the same JComboBox component for both ColumnModel columns which in turn share the same ComboBoxModel. Any change in the selected item from one column will be reflected in the other column. Create a second combobox

    JComboBox comboBox2 = new JComboBox(petStrings);
    ...
    table.getColumnModel().getColumn(1).setCellEditor(editor2);
    

    so that any changes can occur independently in either column.