我正在创建一个嵌套JTable
的程序。为了创建JTable
,我创建了AbstractTableModel
来覆盖方法setValueAt
,isCellEditable
等(代码在底部发布)。我还在类中添加了tableModelListener
方法tableChanged
,该方法保持表通知单元格值已更改。当我将单元格值更改为另一个值时,侦听器可以正常工作并更改单元格值。问题是,当我将单元格值更改为null(实际上删除单元格的值)时,侦听器不起作用,并且值保持不变。我知道如何解决它?
Class Cell:
public class Cell {
private int x;
private int y;
private String content;
public Cell(int x, int y){
this.x = x;
this.y = y;
this.content = content;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public String getContent(){
return content;
}
public void setContent(String content){
this.content = content;
}
}
类CreateTable:
import javax.swing.table.AbstractTableModel;
import javax.swing.undo.UndoManager;
public class CreateTable extends AbstractTableModel {
private int nrows=1000;
private int ncolumns=26;
private String[] columnNames;
private Cell[][] data;
public CreateTable(int nrows,int ncolumns){
this.nrows = nrows;
this.ncolumns = ncolumns;
this.columnNames= new String[ncolumns];
this.data = new Cell[nrows][ncolumns];
for (int i=0;i<ncolumns;i++){
columnNames[i] = super.getColumnName(i);
}
for (int i=0;i<data.length;i++){
for (int j=0;j<columnNames.length;j++){
data[i][j] = new Cell(i,j);
}
}
}
public CreateTable(){
this.columnNames= new String[ncolumns];
this.data = new Cell[nrows][ncolumns];
for (int i=0;i<ncolumns;i++){
columnNames[i] = super.getColumnName(i);
}
for (int i=0;i<data.length;i++){
for (int j=0;j<columnNames.length;j++){
data[i][j] = new Cell(i,j);
}
}
}
public int getColumnCount() {
// TODO Auto-generated method stub
return columnNames.length;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return data.length;
}
@Override
public String getValueAt(int row, int col) {
// TODO Auto-generated method stub
return data[row][col].getContent();
}
public void setValueAt(Object value,int row,int col){
if(value == null || value.toString().isEmpty()){
return;
}
data[row][col].setContent(value.toString());
fireTableCellUpdated(row,col);
}
public boolean isCellEditable(int row, int col){
return true;
}
public void setRowCount(int nrows){
}
public int findColumn(String columnName){
for (int i=0;i<getColumnCount();i++){
if (columnName.equals(columnNames[i])){
return i;
}
}
return -1;
}
}
表格监听器的类表的一部分:
public class Sheet extends JTable {
private int nrows;
private int ncolumns;
private String name;
public Sheet(String name,int nrows,int ncolumns){
this.name = name;
this.setModel(new CreateTable(nrows,ncolumns));
this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
this.getModel().addTableModelListener(e->{
int row = this.getEditingRow();
int column = this.getEditingColumn();
});
this.setCellSelectionEnabled(true);
}
}
答案 0 :(得分:2)
当我将单元格值更改为null(实际上删除单元格的值)时,侦听器不起作用且值保持不变。
正如MadProgrammer在评论中提到的,你的问题在
public void setValueAt(Object value,int row,int col){
if(value == null || value.toString().isEmpty()){
return;
}
data[row][col].setContent(value.toString());
fireTableCellUpdated(row,col);
}
如果值为非空或空字符串,则将单元格的内容设置为该字符串。但是,如果字符串为空(不确定它是如何null
),则不执行任何操作,因此当前值保持不变。
你想要的可能是
public void setValueAt(Object value,int row,int col){
if(value == null || value.toString().isEmpty()){
data[row][col].setContent("");
}
else {
data[row][col].setContent(value.toString());
}
fireTableCellUpdated(row,col);
}
但由于true
的{{1}}和false
条件相同,所以只需执行
if
(同样,除非public void setValueAt(Object value,int row,int col){
data[row][col].setContent(value.toString());
fireTableCellUpdated(row,col);
}
能以某种方式获得value
。)