How to make JTable cell editable/noneditable dynamically?

前端 未结 2 1096
忘了有多久
忘了有多久 2021-01-12 00:46

Is there any way to make non editable cell dynamically in jtable ? Whenever user gives input like false, i want to make non editable cell...I have seen in DefaultTableModel

2条回答
  •  一向
    一向 (楼主)
    2021-01-12 01:19

    I had similar problems to figure out how to enable/disable editing of a cell dynamically (in my case based on occurences in a database.) I did it like this:

    jTableAssignments = new javax.swing.JTable() {
    public boolean isCellEditable(int rowIndex, int colIndex) {
        return editable;
    }};
    

    That of course overrides isCellEditable. The only way I could make that work by the way, was to add the declaration to the instantiation of the tabel itself and not the table model.

    Then I declared editable as a private boolean that can be set e.g.:

        private void jTableAssignmentsMouseClicked(java.awt.event.MouseEvent evt) {                                               
        if(jTableAssignments.getSelectedRow() == 3 & jTableAssignments.getSelectedColumn() == 3) {
            editable = true;
        }
        else {
            editable = false;
        } 
    
    }                                              
    

    And it works quite well.

提交回复
热议问题