1

I have looked at so many tutorials/examples (I've spent a LOT of time here http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) and I still can't figure out how to properly use a JTable to represent data (it's all strings) that changes at runtime. I do NOT want it to be modifiable by selecting rows/cells. The data is modified by my program at runtime (including the amount of rows) AFTER the table is initially created (it's created empty).

I tried an inner class

private class PairingsTableModel extends AbstractTableModel {
 ...
 public void setValueAt(String value, int row, int col) {
  data[row][col] = value;
  fireTableCellUpdated(row, col);
 }
 ...
}

and my JTable is instantiated as follows

JTable pairingsTable = new JTable(new PairingsTableModel());

And it uses the data I provide as instance variables for pairingsTableModel but pairingsTable.getModel() doesn't seem to return the pairingsTableModel (calling pairingsTable.getModel().setValueAt("Hello",0,0) does nothing and Eclipse thinks I'm not using setValueAt anywhere)

3
  • 1
    For better help sooner post an MCVE. Commented Mar 9, 2015 at 12:39
  • 1
    Why are you creating a custom TableModel? The DefaultMethod implements all the method you appear to need, setValueAt(), addRow(...), removeRow(...) etc... Commented Mar 9, 2015 at 14:59
  • @Micromancer Does my answer solve your problem? Do you need any more information? Commented Mar 10, 2015 at 8:19

1 Answer 1

1

You have not correctly overridden setValueAt(...), you've actually overloaded by using a parameter of String rather than Object...

This means that the following calls TableModel.setValueAt(Object aValue, int rowIndex, int columnIndex) not your custom method setValueAt(String aValue, int rowIndex, int columnIndex)

pairingsTable.getModel().setValueAt("Hello", 0, 0) 

Change your setValueAt() to the following, and things should start worked as expected

public void setValueAt(Object value, int row, int col) {
    data[row][col] = (String)value;
    fireTableCellUpdated(row, col);
}

Full MCVE

private static class PairingsTableModel extends AbstractTableModel {
    private String[][] data = new String[][] { new String[] { "foo", "bar" } };
    public void setValueAt(Object value, int row, int col) {
        data[row][col] = (String)value;
        fireTableCellUpdated(row, col);
    }
    @Override
    public int getRowCount() {
        return data.length;
    }
    @Override
    public int getColumnCount() {
        return 1;
    }
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return data[rowIndex][columnIndex];
    }
}
private static int clock;

public static void main(String args[]) {
    JFrame frame = new JFrame();
    final PairingsTableModel model = new PairingsTableModel();
    JTable pairingsTable = new JTable(model);
    frame.setLayout(new BorderLayout());
    frame.add(pairingsTable, BorderLayout.CENTER);
    frame.setVisible(true);
    new Timer().scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            pairingsTable.getModel().setValueAt("test" + clock++, 0, 0);
        }
    }, 0, 1000);
}
Sign up to request clarification or add additional context in comments.

1 Comment

1+, Use @Override before a method you are trying to override. If you override the method signature incorrectly you will get a compiler error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.