Editable JTable Tutorial

前端 未结 6 968
名媛妹妹
名媛妹妹 2021-01-31 06:09

Are there any good books or website that go over creating a JTable? I want to make one column editable. I would like to actually put a inherited JCheckBox

6条回答
  •  你的背包
    2021-01-31 07:02

    in your table Model, you should override "isCellEditable" and "setValueAt" functions, like below.
    Column 4 is the column for editable cells.
    Then when you double click the cell and type something,
    setValueAt() will be called and save the value to the tableModel's DO,field col4.

    public ArrayList tbmData = new ArrayList(); //arraylist for data in table
    
    @Override
    public boolean isCellEditable(int row, int col) {
        if (col == 4) {
            return true;
        } else {
            return false;
        }
    }
    
    @Override
    public void setValueAt(Object value, int row, int col) {
        if ((row >= 0) && (row < this.tbmData.size()) && (col >= 0) && (col < this.colNm.length)) {
            if (col == 4) {
                tbmData.get(row).col4= (String) value;
            }
            fireTableCellUpdated(row, col);
        } else {
        }
    }
    

提交回复
热议问题