如何动态地使JTable单元编辑/不可编辑?

时间:2022-09-12 09: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 isCellEditable method.But if i want to use that i have create each time new object.So i want to change it non editable dynamically. Can you anyone please help me?..thanks

是否有方法可以使不可编辑的单元格在jtable中动态地生成?每当用户输入错误的时候,我想要做一个不可编辑的单元格…我在DefaultTableModel isCellEditable方法中见过。但如果我想使用,我已经创建了每一个新的对象。所以我想动态地修改它。你能帮帮我吗?

2 个解决方案

#1


17  

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 class

其他类

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

You can then set the values dynamically by using the myModel variable you have stored and calling the setCellEditable() function on it.. in theory. I have not tested this code but it should work. You may still have to fire some sort of event to trigger the table to notice the changes.

然后,您可以使用存储的myModel变量并调用setCellEditable()函数来动态地设置值。理论上是这样。我没有测试过这个代码,但是它应该可以工作。您可能仍然需要触发一些事件来触发表来注意更改。

#2


2  

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.

当然,这可以推翻isCellEditable。顺便说一下,我唯一能做的工作就是将该声明添加到tabel本身的实例化中,而不是在表模型中。

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.

而且效果很好。

#1


17  

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 class

其他类

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

You can then set the values dynamically by using the myModel variable you have stored and calling the setCellEditable() function on it.. in theory. I have not tested this code but it should work. You may still have to fire some sort of event to trigger the table to notice the changes.

然后,您可以使用存储的myModel变量并调用setCellEditable()函数来动态地设置值。理论上是这样。我没有测试过这个代码,但是它应该可以工作。您可能仍然需要触发一些事件来触发表来注意更改。

#2


2  

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.

当然,这可以推翻isCellEditable。顺便说一下,我唯一能做的工作就是将该声明添加到tabel本身的实例化中,而不是在表模型中。

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.

而且效果很好。