Putting JComboBox into JTable

前端 未结 9 1285
离开以前
离开以前 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));
    }
    
    0 讨论(0)
  • 2020-11-30 10:57

    You need to create a subclass of JTable to override the method TableCellEditor getCellEditor(int row, int column).

    This enables you to set arbitrary cell editors for any row and column combination. The default way is to set the cell editor for an entire column.

    (You can also set individual cell renderers by overriding getCellRenderer.)

    0 讨论(0)
  • 2020-11-30 10:58

    This page might help you, although it seems you are restricted to having the same combobox in all the cells in a column.

    0 讨论(0)
提交回复
热议问题