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

后端 未结 2 1441
终归单人心
终归单人心 2020-11-28 14:51

I am trying to add unique JComboBoxes to a column in a JTable. I know it is possible to add a JComboBox to an entire column using

2条回答
  •  抹茶落季
    2020-11-28 15:27

    Try this:

    ...
    final Object[] obj = {"test1", "test2", "test3"};
    JTable jTable = new JTable();
    TableColumn column = jTable.getColumnModel().getColumn(1);
    column.setCellEditor(new AutoCompletionComboBoxEditor(obj));
    ...
    
    public static class AutoCompletionComboBoxEditor extends AbstractCellEditor implements TableCellEditor {
    
            JComboBox cbx;
    
            public AutoCompletionComboBoxEditor(Object[] items) {
                cbx = new JComboBox(items);
            }
    
            public Component getTableCellEditorComponent(JTable table,
                    Object value, boolean isSelected, int row, int column) {
                return cbx;
            }
    
            public Object getCellEditorValue() {
                return cbx.getSelectedItem();
            }
        }
    

    try tell me the effect :)

    -Saligh

提交回复
热议问题