Hi
but i should have the Table as property in the class, because many methodes will use it
class myClass{
String[] columnNamesA = {"col 1", "col 2","col 3"};
Object[][] dataA = {"1","2","3"
,"4","5","6"};
JTable tableA= new JTable(dataA, columnNamesA);
public update()
{
tableA.getModel().setValueAt("12", 0, 1 );
}
}
it didn't work can you help me!
i have problem <identifier> expected
class myClass extends JPanel implements TableModelListener {
String[] columnNamesA = {"col 1", "col 2","col 3"};
Object[][] dataA = {"1","2","3"
,"4","5","6"};
JTable tableA= new JTable(dataA, columnNamesA);
tableA.getModel().addTableModelListener(this); // error here <identifier>expected
public update()
{
tableA.getModel().setValueAt("12", 0, 1 );
}
}
The line with the error has to be inside some method or constructor:// Use capital letters to start class names
class MyClass extends JPanel implements TableModelListener {
String[] columnNamesA = {"col 1", "col 2","col 3"};
Object[][] dataA = {"1", "2", "3", "4", "5", "6"};
JTable tableA= new JTable(dataA, columnNamesA);
// The initialisation of columnsNamesA, dataA and
// tableA could go in the constructor as well
MyClass()
{
tableA.getModel().addTableModelListener(this);
}
public update()
{
tableA.getModel().setValueAt("12", 0, 1 );
}
// A TableModelListener has to have a method like this
public void tableChanged(TableModelEvent e)
{
}
}
> Now i have no compilation errors
> but, the update didn't appear in the table
There's nothing in your code so far that would cause the table's data to be
updated. The line of code you are using in update() looks OK, but update()
is not being invoked at any time. No update(), no change in the table.
A TableModelListener is probably the wrong sort of thing to be updating the
table. As its name suggests, a TableModelListener listens to changes
in the data that a table contains. It isn't designed to cause the table to change,
rather it responds to changes initiated elsewhere.
It looks like you need:
(1) A JFrame to display
(2) A JTable to put in the frame
(3) A JButton or some other control to initiate the data change.
The button (or whatever) will use the line of code that you currently have
in update() to change the data in the table. And the table (which is a
TableModelListener) will update itself.
There is a nice example of all this in Sun's tutorial here:
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#modelchange
And a couple of other things:
You'll get the best help for this sort of thing in the Swing forum.
Personally I think JTable with its model and listeners and things is about the
trickiest of the Swing components. Going through the tutorial (and working out
your own examples) is not easy. But is worthwhile.