Search code examples
javaswingjtablejcomboboxtablecelleditor

Celleditor (JComboBox) in a specific row of a JTable


I don't know how to do to set a jcombobox in a specific row...for now I've this jcombobox for all rows but I want it in only one row:

JComboBox cc = new JComboBox();
cc.addItem(jComboBox5.getSelectedItem()+"/"+jComboBox6.getSelectedItem()+"/"+jComboBox7.getSelectedItem()+" "+jComboBox1.getSelectedItem()+"."+jComboBox2.getSelectedItem());
jTable1.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(cc));
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setToolTipText("CLICCA PER LE DATE");
jTable1.getColumnModel().getColumn(3).setCellRenderer(renderer);

Solution

  • Update: As I was testing my (probably incomplete) answer out, I came across a very good SO Question that I think will help much better than I could: Putting JComboBox into JTable

    Another Update: I read your question again, and I realized you asked for a specific row. The only way I can think of doing this is to create a custom CellEditor, something like:

    private static class MyCellEditor extends AbstractCellEditor implements TableCellEditor {
    
        DefaultCellEditor other = new DefaultCellEditor(new JTextField());
        DefaultCellEditor checkbox = new DefaultCellEditor(new JComboBox(new Object[] {"abc"}));
    
        private DefaultCellEditor lastSelected;
    
        @Override
        public Object getCellEditorValue() {
    
            return lastSelected.getCellEditorValue();
        }
    
        @Override
        public Component getTableCellEditorComponent(JTable table,
                Object value, boolean isSelected, int row, int column) {
            if(row == 0) {
                lastSelected = checkbox;
                return checkbox.getTableCellEditorComponent(table, value, isSelected, row, column);
            }
            lastSelected = other;
            return other.getTableCellEditorComponent(table, value, isSelected, row, column);
        }
    
    }
    

    In this example, the custom CellEditor is actually two Editors, and depending on the row selected, the particular Editor will get the call (both figuratively and literally). I admit that the lastSelected seemed a bit hokey, but I honestly could not find an easier way to know which Editor value to return (as the getCellEditorValue has no args).

    To make your Table appear "correct", you're probably going to have to do something with the Renderer as well (because the Renderer may or may not know to show the JComboBox's selected value as the initial value). This depends on how you're initializing the data in the actual table.


    For completeness, my original answer is below:

    You can add the JComboBox component to the row using addRow on the TableModel as shown here: How to add row in JTable?

    See also: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

    I think the main issue is you're mixing the idea of Column Editors/Renderers with the actual data that will be stored in each row.