Putting JComboBox into JTable

前端 未结 9 1293
离开以前
离开以前 2020-11-30 10:22

I want to put individual JComboBoxes into each cells of a JTable. ie. The JComboBox content is not identical for each cell.

I basically would like to be able to jus

9条回答
  •  情话喂你
    2020-11-30 10:52

    I am sure this will solve your problem. Mention in which column you need to set the combo box in .getColumn(int column)

    private void addComboToTable(JComboBox combo) {
        TableColumn gradeColumn = YourTable.getColumnModel().getColumn(0);
        JComboBox comboBox = combo;
        comboBox.removeAllItems();
        try {
            comboBox.addItem("Item 1");
            comboBox.addItem("Item 2");
            comboBox.addItem("Item 3");
        } catch (NullPointerException e) {
        } catch (Exception e) {
            e.printStackTrace();
        }
        gradeColumn.setCellEditor(new DefaultCellEditor(comboBox));
    }
    

提交回复
热议问题