Search code examples
javaswingjtablejcombobox

JComboBox shared between multiple table cells automatically selecting currently selected item


I have added a combobox as a cell editor using the code provided by camickr below as reference:

How to add unique JComboBoxes to a column in a JTable (Java)

Except in my case, I only need one combobox to be used by all of the cells within a column. The problem I'm running into is that the combobox automatically selects the last selected item (or current selected item, not sure), and since different rows share the same combobox, if you click on one of the cells, it'll automatically change to the last selected item.

As a quick demonstration I simply modified the code from above to show the issue. I would like the combo box to automatically select an item on the list equal to the item that is set in selected cell (as opposed to selecting a cell, and then having the contents of that cell automatically change)

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;

public class Test extends JFrame
{
    List<TableCellEditor> editors = new ArrayList<TableCellEditor>(3);

    public Test()
    {
        // Create the editors to be used for each row

        String[] items1 = { "Red", "Blue", "Green" };
        JComboBox comboBox1 = new JComboBox( items1 );
        DefaultCellEditor dce1 = new DefaultCellEditor( comboBox1 );
        editors.add( dce1 );

        //  Create the table with default data

        Object[][] data =
        {
            {"Color", "Red"},
            {"Shape", "Square"},
            {"Fruit", "Banana"},
            {"Plain", "Text"}
        };
        String[] columnNames = {"Type","Value"};
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        JTable table = new JTable(model)
        {
            //  Determine editor to be used by row
            public TableCellEditor getCellEditor(int row, int column)
            {
                int modelColumn = convertColumnIndexToModel( column );

                if (modelColumn == 1 && row < 3)
                    return editors.get(0);
                else
                    return super.getCellEditor(row, column);
            }
        };

        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
    {
        Test frame = new Test();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }
} 

Solution

  • By default the first item of a combo box is selected, which in your example is "Red".

    When you edit a cell the value from the TableModel is selected in the comboBox editor. Since the data in your table does not match any of the entries in the comboBox, the selection doesn't change so "Red" is displayed as the value in the editor.

    When you make a selection from the editor that value is then saved in the model and will be displayed properly the next time you edit the cell.

    The solution to your problem is to make sure the TableModel contains valid data when it is created. Only that way can the proper item in the comboBox be selected.

    I figured I would have to attach an action listener to the combobox to determine which item to selected

    No, you don't play with listeners on a comboBox when it is used as an editor. The comboBox editor does the selection of the item automatically for you.