0

i've declared a JTable (inside a class extended JPanel constructor) such as

data_table = new JTable(info, header) {
    @Override
    public boolean isCellEditable(int row, int column) {
        //disable table editing
        return false;
    }
};

where info and column are declared

static String[][] info = new String[row][cols];
static String[] header = {"h1", "h2", "h3"};

Now i need to update, when some events occours, the table content by invoke a static method. How can i do it?

giozh
  • 9,868
  • 30
  • 102
  • 183

2 Answers2

4

i don't have a tableModel, i've a matrix of string

All tables use a TableModel. When you create the table the matrix of Strings is used by the TableModel.

To update your data you do something like:

table.setValueAt(...);

This will cause the model to be updated and the model will tell the table to repaint itself.

Read the Swing tutorial on How to Use Tables for more information about tables.

Also, you should NOT be using static variables or method. If you are then you program is poorly designed. Again read the tutorial for a better example of how to structure your code.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • sorry, i'm not explain my problem so well. I don't need to modify ad existing data row, but i need to "append" some new rows to JTable. – giozh Jul 17 '13 at 15:20
  • Use the `addRow(...)` method of the `DefaultTableModel`. – camickr Jul 17 '13 at 15:30
-3

If you want to notify your JTable about changes of your data, call a method on your tablemodel that will update the data and call fireTableDataChanged()

Its is a good practice to use a JTableModel to maintain your data inside the JTable. http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

And that is your tablemodel that will fire the fireTableDataChanged() when value will change.

class MyTableModel extends AbstractTableModel {
    private String[] columnNames = ...//same as before...
    private Object[][] data = ...//same as before...

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

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

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

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

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col < 2) {
            return false;
        } else {
            return true;
        }
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) {
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    }
    ...
}
fluminis
  • 3,575
  • 4
  • 34
  • 47