2

I am trying to set a value in my jtablemodel using the setValueAt method whenever a header is clicked. I currently have a MouseAdapter correctly displaying which column is being clicked on but the board.setValueAt() method is not being called.

Mouse Listener. This correctly displays the column in which I click. I've tried to use board.getModel().setValueAt() but that didnt work either.

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. The "Set value at entered" never gets displayed to console. When I try to Override it it gives me an error.

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());
    }

}

Main method board creation...

JTable board = new JTable(new TableModel());
board.getTableHeader().addMouseListener(new HeaderListener(board));

Can anyone tell me what I am doing wrong?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Chad
  • 104
  • 1
  • 2
  • 6
  • Why are you creating a custom TableModel. You are not adding any special functionality so you should be able to use the `DefaultTableModel`. Also, don't call your class TableModel, the is the interface name so it is confusing. The class name should be more descriptive. – camickr Oct 18 '14 at 17:34

1 Answers1

5

Your setValueAt method signature does not match that of the AbstractTableModel and thus does not override the super's own method. The first parameter should be Object, not Data. Always use the @Override annotation when overriding methods to allow the compiler to check to be sure that you're doing it correctly.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thanks man, this was it. I made it Object data2 and cast it it to (Data) in the method. Now I can do the Override. I didn't realize I had to use Object instead. – Chad Oct 18 '14 at 17:34
  • 1
    @Chad: that's why the `@Override` is so important. You don't have to realize it, but with the annotation, the compiler will inform you that it's not right. – Hovercraft Full Of Eels Oct 18 '14 at 17:35
  • 1
    @Chad, `that's why the @Override is so important.` it also shows why it is important to be consistent when coding. All the other methods use the @Override annotation. It is sloppy/lazy not to make sure all the methods use it. – camickr Oct 18 '14 at 17:37
  • I knew it was suppose to be @Override but I couldn't figure out why it was giving me an error recommending me to remove it. Now I know you can't change the param types. – Chad Oct 18 '14 at 17:42