0

I need to put JComboBox into JTable. The JComboBox should contain a list of entries corresponding to particular row. For instance:

    Position          |   Skills
    Programmer        |   List<String{Java,C,C++}
    Web Programmer    |   List<String{javaScript,PHP,MySQL}

I populate the table with Object[][] data. One of columns of data contains List<String>.

I wrote the following renderer. However, the problem is that it outputs the content of JComboBox in each row is the same.

    DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer() 
    {
        public Component getTableCellRendererComponent( JTable table, Object value, boolean
                                                        isSelected, boolean hasFocus, int row, int column)
        {
            super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column );
            if ( value instanceof List<?>)
            {
                int vColIndex = 5;
                TableColumn col = table.getColumnModel().getColumn(vColIndex);
                col.setCellEditor(new ComboBoxEditor(((ArrayList<?>) value).toArray()));
                col.setCellRenderer(new ComboBoxRenderer(((ArrayList<?>) value).toArray()));
            }

            return this;
        }
    };

    table = new JTable(model);
    for (int i = 0; i < table.getColumnCount(); ++i) 
    {
        table.getColumnModel().getColumn(i).setCellRenderer(cellRenderer);
    } 

ComboBox renderers:

class ComboBoxRenderer extends JComboBox implements TableCellRenderer {
    public ComboBoxRenderer(Object[] objects) {
        super(objects);
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        if (isSelected) {
            setForeground(table.getSelectionForeground());
            super.setBackground(table.getSelectionBackground());
        } else {
            setForeground(table.getForeground());
            setBackground(table.getBackground());
        }

        // Select the current value
        setSelectedItem(value);
        return this;
    }
}

class ComboBoxEditor extends DefaultCellEditor {
    public ComboBoxEditor(Object[] objects) {
        super(new JComboBox(objects));
    }
}
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
  • 1
    Your code is strange you are creating new CellRenderers and CellEditors inside the cellRenderer. Did you read [Using a comboBox as an editor](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#combobox) – nachokk Mar 20 '14 at 17:43
  • @nachokk: yes,but I don't understand how to create different content of JComboBoxes depending on the row. – Klausos Klausos Mar 20 '14 at 17:48
  • 3
    See the related example, [`TableComboBoxByRow`](http://stackoverflow.com/a/3256602/230513). – trashgod Mar 20 '14 at 17:50

1 Answers1

0
    JComboBox box=new JComboBox(values);
    TableColumn col = table.getColumnModel().getColumn(0);
    col.setCellEditor(new DefaultCellEditor(box));
Toufic
  • 292
  • 3
  • 8