每次点击标题时,我都会尝试使用setValueAt方法在jtablemodel中设置一个值。我目前有一个MouseAdapter正确显示正在单击哪个列但是没有调用board.setValueAt()方法。
鼠标侦听器。这会正确显示我单击的列。我尝试过使用board.getModel()。setValueAt(),但这也没用。
public class HeaderListener extends MouseAdapter{
private JTable board;
public HeaderListener(JTable board){
this.board = board;
}
@Override
public void mouseClicked(MouseEvent event) {
Point point = event.getPoint();
int column = board.columnAtPoint(point);
System.out.println(column);
Data value = new Data(0,"yes");
board.setValueAt(value, 0, column);
}
}
TableModel的即可。输入"设置值"永远不会显示到控制台。当我试图覆盖它时,它给了我一个错误。
public class TableModel extends AbstractTableModel{
private Data[][] data;
private String[] columnNames = {"Move Here",
"Move Here",
"Move Here",
"Move Here",
"Move Here","Move Here"};
public TableModel(){
data = new Data[6][7];
Data test = new Data(0,"yes");
data[1][1] = test;
}
@Override
public int getRowCount() {
return data.length;
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public String getColumnName(int col) {
return columnNames[col];
}
@Override
public Object getValueAt(int row, int col) {
return (data[row][col]==null?"":data[row][col].getText());
}
public void setValueAt(Data data2, int row, int col){
System.out.println("Set value at entered");
data[row][col] = data2;
fireTableCellUpdated(row,col);
System.out.println(data[row][col].getText());
System.out.println(data[row][col].getPlayer());
}
}
主要方法板创建......
JTable board = new JTable(new TableModel());
board.getTableHeader().addMouseListener(new HeaderListener(board));
谁能告诉我我做错了什么?
答案 0 :(得分:5)
您的setValueAt方法签名与AbstractTableModel的签名不匹配,因此不会覆盖超级自己的方法。第一个参数应该是Object,而不是Data。在重写方法时始终使用@Override
注释,以允许编译器检查以确保您正确执行此操作。