There are a lot of articles on how to implement JTable that can't be edited, because if you don't, double-clicking the cell will read the contents of the cell into a default editor, and many times we don't want it to happen, so we have to do something to stop it. Oh, well, I admit that we all know that the following method can be implemented so that the entire table is not allowed to be edited
DefaultTableModel Newtablemodel = new DefaultTableModel (datavector,columnmodelvector) {
@Override
public Boolean iscelleditable (int row,int column) {return
false;
}
};
There is nothing mystical about rewriting the DefaultTableModel method isCellEditable method, but in more cases, we need to specify that a column or columns in the table are allowed to be edited, such as the number of items in the list, the price, and so on, so how do you implement it?
In fact, a closer look, this iscelleditable method will pass two parameters come in, a discerning eye to know that this is the cell row and column index, so, according to this row and columns to control return TRUE or FALSE, our desire can be achieved. Here are a few examples to learn to extrapolate OH:
1, only allow the third column of the table to be edited
public boolean iscelleditable (int row,int column) {
if (column = = 3) {return
true;
} else{return
false;
}
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/
2, only allow the second row of the table, the second column of cells are edited
public boolean iscelleditable (int row,int column) {
if (row = = 2 && column = = 2) {return
true;
} else{return
false;
}
3. Allow only the 4th and 6th of the table to be edited
public boolean iscelleditable (int row,int column) {
if (row = = 4 | | | row = = 6) {return
true;
} else{return
false;
}