0

I use a AbstractTableModel for my JTable.

public class MyTableModel extends AbstractTableModel {

     private List<String> columnNames = new ArrayList<String>();
     private List<List> data = new ArrayList();

     MyTableModel() {
          columnNames.add("Action");
          columnNames.add("Count");
          columnNames.add("Total");
          columnNames.add("X");
          columnNames.add("Y");
          columnNames.add("Quarter");
          columnNames.add("Answer");
          columnNames.add("T (ms)");
     }

     public void addRow(List rowData) {
          data.add(rowData);
          fireTableRowsInserted(data.size() - 1, data.size() - 1);
     }

     public void insertRow(int index, List rowData) {
          data.add(0, rowData);
          fireTableRowsInserted(data.size() - 1, data.size() - 1);
     }

     public int getColumnCount() {
          return columnNames.size();
     }

     public int getRowCount() {
          return data.size();
     }

     public String getColumnName(int col) {
          try {
               return columnNames.get(col);
          } catch (Exception e) {
               return null;
          }
     }

     public Object getValueAt(int row, int col) {
          return data.get(row).get(col);
     }


     public boolean isCellEditable(int row, int col) {
          return false;
     }

     public Class getColumnClass(int c) {
          return getValueAt(0, c).getClass();
     }     
}

I add to it (with success) new rows while application is running by execute some code in a some not important class. It looks like this.

MyClass.MyTableModel.insertRow(0, (Arrays.asList(String.valueOf(valueOne), String.valueOf(valueTwo), String.valueOf(valueThree), 
                    String.valueOf(valueFour), String.valueOf(valueFive), String.valueOf(valueSix), String.valueOf(valueSeven), 
                    String.valueOf(valueEight), String.valueOf(valueNine))));
MyClass.myTable.setModel(MyClass.MyTableModel);
MyClass.myTable.repaint();

But I can't change data in specific cells in such added rows. I tried to cover a setValueAt() method, but with no success. Also I tried to simply call method setValueAt() for MyClass.MyTableModel for example:

MyClass.MyTableModel.setValueAt("valueChange", 0, 6);
MyClass.myTable.setModel(MyTableModel);
MyClass.myTable.repaint();

That also doesn't work. I tried to call fireTableCellUpdated() for my model (i know it's wrong practice), but it also failed in attempt. Do you have any ideas?

bluevoxel
  • 4,978
  • 11
  • 45
  • 63
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Oct 02 '13 at 21:13
  • Please use the right capitalization for variable names to increase readability; variables should be begin with a small letter, unlike `MyClass` and its field `MyClass.MyTable`. "Doesn't work" is not a very helpful error description. – Hauke Ingmar Schmidt Oct 02 '13 at 21:31
  • @AndrewThompson, I'll remember, thank you sir. Mr his, how could I better describe situation when data update doesn't work and I don't know why? If I knew why there wouldn't be that question. – bluevoxel Oct 02 '13 at 21:46
  • A tip: Only one person can be notified in a single comment (you may have already noticed that). But for a 2nd person to be notified, break the comment 'in two'. – Andrew Thompson Oct 02 '13 at 23:00
  • @eLogic "It doesn't do anything, nothing happens" is something essentially different than "It doesn't work" or "with no success". The latter could e.g. mean that exceptions are thrown (and suppressed). – Hauke Ingmar Schmidt Oct 02 '13 at 23:11
  • @his, yes, now I see your point. Next time I will give more detailed description of the problem. Thank you. – bluevoxel Oct 03 '13 at 00:12

1 Answers1

2

You don't override the setValueAt method. By default, this method does nothing.

For example...

public void setValueAt(Object value, int row, int column) {
    List rowValues = date.get(row);
    rowValues.set(column, value);
    fireTableCellUpdated(row, col);
}

This assumes that all the columns are editable. Normally I would inspect the incoming value to ensure it was the correct type for the specified column and apply what ever validation might need to be done, but I'll leave that up to you

You might also like to take a look at How to use tables which as a few examples

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I didn't try to get whole row data in the list... Your solution works perfect. Thank you sir very much! – bluevoxel Oct 02 '13 at 22:16