-
non editable JTable?
Is there anyway i can check to see if the cells in a JTable are editable? If i pull up a JTable loaded with data in it's colums i can manually click on a cell and retype new values into the cell, tab off to another cell then go back and edit again. Point is that it's always editable. Now when i hook a table up to a backend as soon an the data changes in the table it becomes non-editable. Any reason for this?
-
you can get the model out of the JTable and there is a method on it called isCellEditable(int row, int index) just pass int eh coordinates you want to test and it will return true or false....
-
Ok what do i do then? :lol: I don't see any setEditable(int col int row,boolean editable) or a setEditable(boolean editable) methods to set either a specific cell or the whole JTable editable.
-
ahh, sorry, i thought you just wanted to test if a cell was editable, not disable and enabled editing full stop on the table...
if i wanted this kind of functionality i personally would write a TableModel Delegate, that could accept anoter TableModel, and add methods that would allow me to set the entire table editable or not.
Code:
public class MyTable extends JTable{
ModelDelegate delegate = new ModelDelegate();
boolean editable = true;
public void setModel(TableModel dataModel) {
delegate.setModel(dataModel);
super.setModel(this.delegate);
}
public void setEditable(boolean editable){
this.editable = editable;
}
private class ModelDelegate implements TableModel{
TableModel model;
public void setModel(TableModel model){
this.model = model;
}
public void addTableModelListener(TableModelListener l) {
model.addTableModelListener(l);
}
public boolean equals(Object obj) {
return model.equals(obj);
}
public Class getColumnClass(int columnIndex) {
return model.getColumnClass(columnIndex);
}
public int getColumnCount() {
return model.getColumnCount();
}
public String getColumnName(int columnIndex) {
return model.getColumnName(columnIndex);
}
public int getRowCount() {
return model.getRowCount();
}
public Object getValueAt(int rowIndex, int columnIndex) {
return model.getValueAt(rowIndex, columnIndex);
}
public int hashCode() {
return model.hashCode();
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
/**
* Test to see if the Table is editable
* if so call in the model we are delegtaing to
*/
if(editable){
return model.isCellEditable(rowIndex, columnIndex);
}else{
/**
* Return false.
*/
return editable;
}
}
public void removeTableModelListener(TableModelListener l) {
model.removeTableModelListener(l);
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
model.setValueAt(aValue, rowIndex, columnIndex);
}
}
}
would that do what you wanted?