我想在每次單擊標題時使用setValueAt方法在我的jtablemodel中設置一個值。我目前有一個MouseAdapter正確顯示哪個列被點擊,但board.setValueAt()方法沒有被調用。AbstractTableModel 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));
誰能告訴我什麼,我做錯了什麼?
你爲什麼要創建一個自定義TableModel。你沒有添加任何特殊的功能,所以你應該可以使用'DefaultTableModel'。另外,不要調用你的類TableModel,它是接口名稱,所以它很混亂。類名應該更具描述性。 – camickr 2014-10-18 17:34:44