Java – how to make dynamic JTable cells editable / non editable?

Is there any way to dynamically create non editable cells in JTable? Whenever the user gives input like false, I want to make non editable cells... I've seen in the defaulttablemodel iscelleditable method But if I want to use it, I've created every new object So I want to change it. It can't be dynamically edited Could you help me? thank you

Solution

public class MyDefaultTableModel extends DefaultTableModel {
public class MyDefaultTableModel extends DefaultTableModel {
    private boolean[][] editable_cells; // 2d array to represent rows and columns

    private MyDefaultTableModel(int rows,int cols) { // constructor
        super(rows,cols);
        this.editable_cells = new boolean[rows][cols];
    }

    @Override
    public boolean isCellEditable(int row,int column) { // custom isCellEditable function
        return this.editable_cells[row][column];
    }

    public void setCellEditable(int row,int col,boolean value) {
        this.editable_cells[row][col] = value; // set cell true/false
        this.fireTableCellUpdated(row,col);
    }
}

Other classes

... stuff
DefaultTableModel myModel = new MyDefaultTableModel(x,y); 
table.setModel(myModel);
... stuff

You can then dynamically set the value using the stored mymodel variable and call the setcelleditable () function on it theoretically. I didn't test this code, but it should work You may still need to trigger an event to trigger the table to notice these changes

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>