当前位置: 技术问答>java相关
一个简单的问题:如何设置jTable使表格不可编辑,但可以选取行?
来源: 互联网 发布时间:2015-03-27
本文导语: 能行的话立马给分!!! | 重新定制TableModel class TableModel extends DefaultTableModel{ Vector dataEntries;; Vector columnTitles; public TableModel(Vector dataEntries,Vector columnTitles){ this.dataEntries ...
能行的话立马给分!!!
|
重新定制TableModel
class TableModel extends DefaultTableModel{
Vector dataEntries;;
Vector columnTitles;
public TableModel(Vector dataEntries,Vector columnTitles){
this.dataEntries = dataEntries;
this.columnTitles = columnTitles;
super.setDataVector(dataEntries,columnTitles);
}
/*public int getRowCount(){
return dataEntries.size();
}*/
public int getColumnCount(){
return columnTitles.size();
}
public Object getValueAt(int row,int column){
return ((Vector)dataEntries.elementAt(row)).elementAt(column);
}
public Class getColumnClass(int column){
return getValueAt(0,column).getClass();
}
/**********************************
isCellEditable设置了第一列和第三列不可编辑!
*/
public boolean isCellEditable(int row,int column){
return column ==0||column ==2?false:true;
}
public void setValueAt(Object data,int row,int column){
super.setValueAt(data,row,column);
}
}
class TableModel extends DefaultTableModel{
Vector dataEntries;;
Vector columnTitles;
public TableModel(Vector dataEntries,Vector columnTitles){
this.dataEntries = dataEntries;
this.columnTitles = columnTitles;
super.setDataVector(dataEntries,columnTitles);
}
/*public int getRowCount(){
return dataEntries.size();
}*/
public int getColumnCount(){
return columnTitles.size();
}
public Object getValueAt(int row,int column){
return ((Vector)dataEntries.elementAt(row)).elementAt(column);
}
public Class getColumnClass(int column){
return getValueAt(0,column).getClass();
}
/**********************************
isCellEditable设置了第一列和第三列不可编辑!
*/
public boolean isCellEditable(int row,int column){
return column ==0||column ==2?false:true;
}
public void setValueAt(Object data,int row,int column){
super.setValueAt(data,row,column);
}
}
|
你用model的话,在model加入
public boolean isCellEditable(int row,int column){
return false;
}
public boolean isCellEditable(int row,int column){
return false;
}