How to make a JComboBox table editor have the design of an ordinary JComboBox?

Working from this complete example as a common frame of reference, note how the appearance of unselected cells in the ITEM_COL column is due to the default renderer. The arrow button typical of a stand-alone JComboBox only appears when the cell’s editor is evoked, as by clicking on the cell or pressing Space when the cell is selected. You can add a triangle in a custom renderer:

final JComboBox combo = new JComboBox(items);
TableColumn col = table.getColumnModel().getColumn(ITEM_COL);
col.setCellRenderer(new DefaultTableCellRenderer(){

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        JLabel label = (JLabel) super.getTableCellRendererComponent(table,
            value, isSelected, hasFocus, row, column);
        label.setIcon(UIManager.getIcon("Table.descendingSortIcon"));
        return label;
    }
});

Addendum: A more complete example due to @aterai is seen here.

Leave a Comment