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

前端 未结 2 519
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 12:01

I have a JComboBox used as an editor in a JTable. In the picture you can see them in the column labeled Produs. I would like to use th

2条回答
  •  春和景丽
    2020-11-29 12:24

    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.

提交回复
热议问题