JTable help

HiHow can i update a JTable?i used :tablename.setValueAt("some data", row, col ); but it didn't workthanks for help
[158 byte] By [J_proga] at [2007-10-3 7:44:13]
# 1
I used this functiontableA.getModel().setValueAt(data, row number,column number);//tableA is an instance of Jtableit works only if I used it in the same function I created the table in !!!!
netsima at 2007-7-15 2:45:26 > top of Java-index,Java Essentials,New To Java...
# 2

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!

J_proga at 2007-7-15 2:45:26 > top of Java-index,Java Essentials,New To Java...
# 3
read this: http://forum.java.sun.com/thread.jspa?forumID=54&threadID=608151and this: http://forum.java.sun.com/thread.jspa?forumID=54&threadID=754058:)i'm learninggood luck
netsima at 2007-7-15 2:45:26 > top of Java-index,Java Essentials,New To Java...
# 4

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

}

}

J_proga at 2007-7-15 2:45:26 > top of Java-index,Java Essentials,New To Java...
# 5

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)

{

}

}

pbrockway2a at 2007-7-15 2:45:26 > top of Java-index,Java Essentials,New To Java...
# 6
Thanks a lotNow i have no compilation errorsbut, the update didn't appear in the tableshould i implement the update inside tableChanged(TableModelEvent e)?
J_proga at 2007-7-15 2:45:26 > top of Java-index,Java Essentials,New To Java...
# 7

> 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

pbrockway2a at 2007-7-15 2:45:26 > top of Java-index,Java Essentials,New To Java...
# 8

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.

pbrockway2a at 2007-7-15 2:45:26 > top of Java-index,Java Essentials,New To Java...
# 9
Thanks a lot :)i appreciate your help :)it worksas you said, i have a problem with calling update()Thanks again :)
J_proga at 2007-7-15 2:45:26 > top of Java-index,Java Essentials,New To Java...