JTable- still problems with refreshing (jdbc)

Hi everybody,

nice to join your group,

I'm trying to create my first database application, but have some problems with the main component - JTable, I created that using TableModel which gets all data from sql database (ResultSet etc.):

table1 = new JTable(new MyModel(rs) );

and MyModel looks like that:

publicclass MyModelextends AbstractTableModel

{

private ResultSet rs;

private ResultSetMetaData rsmd;

public MyModel(ResultSet result)

{

rs=result;

try

{

rsmd=rs.getMetaData();

}

catch(SQLException e)

{

e.printStackTrace();

}

}

public String getColumnName(int col)

{

try

{

return rsmd.getColumnName(col +1);

}

catch(SQLException e)

{

e.printStackTrace();

return"";

}

}

publicint getColumnCount()

{

try

{

return rsmd.getColumnCount();

}

catch(SQLException e)

{

e.printStackTrace();

return 0;

}

}

private ResultSet getResultSet()

{

return rs;

}

public Object getValueAt(int r,int c)

{

try

{

ResultSet rs=getResultSet();

rs.absolute(r+1);

return rs.getObject(c+1);

}

catch (SQLException e)

{

e.printStackTrace();

returnnull;

}

}

publicint getRowCount()

{

try

{

ResultSet rs=getResultSet();

rs.last();

return rs.getRow();

}

catch (SQLException e)

{

e.printStackTrace();

return 0;

}

}

}

Everytime someone elase changes data in sql database its very important to show this data in JTable- can anyone tell me how to do it?

I found some info on sunTutorials about TableModel listener like this one:

table1.getModel().addTableModelListener(new TableModelListener()

{

publicvoid tableChanged(TableModelEvent e){

int row = e.getFirstRow();

int column = e.getColumn();

TableModel model = (TableModel)e.getSource();

String columnName = model.getColumnName(column);

Object data = model.getValueAt(row, column);

}

}

);

which must know what's going on from one of the 'fire' methods (i have checked fireTableStructureChanged() but got lots of errors)- but imo this listener will do something only when i change some data on the JTable element directly. Am I right? so how to let the JTable know about sql database changes?

thanks in advance

excuse my english ;)

regards,

david

[4988 byte] By [davidosXa] at [2007-11-27 10:07:31]
# 1

> Everytime someone elase changes data in sql database its very

> important to show this data in JTable- can anyone tell me how to do it?

There is no way to do it. The SQL database does not nofity your application when data is changed. So all you can do is periodically query the database and refresh the entire JTable.

A TableModelListener is only used to notify you when a change is made to the JTable directly. It knows nothing about the SQL database where the data originally came from.

camickra at 2007-7-13 0:43:49 > top of Java-index,Desktop,Core GUI APIs...
# 2
So how to refresh JTable? repaint() method is not working. ?
davidosXa at 2007-7-13 0:43:49 > top of Java-index,Desktop,Core GUI APIs...
# 3
You change the data in the TableModel and the table repaints itself.
camickra at 2007-7-13 0:43:49 > top of Java-index,Desktop,Core GUI APIs...
# 4
But what do you mean 'change data in the TableModel' ?- there is only method which connects to database and shows data on JTable (getValueAt()). Sory, but that's my first application- can u tell me exactly how to do this please?
davidosXa at 2007-7-13 0:43:49 > top of Java-index,Desktop,Core GUI APIs...
# 5
If you are getting all new data from the database then the easiest way to do it is to create a new table model and then reset the TableModel used by the table by using:table.setModel(...);
camickra at 2007-7-13 0:43:49 > top of Java-index,Desktop,Core GUI APIs...
# 6

Thanks a lot camickr ! it works without any problems!

ByTheWay I use eclipse. I've added to my project external sources like jdbc or jacob libs, also selected in properities 'export with sources' - any ideas why after generating 'export' jar file, it cannot be launched on other machine with Virtual Machine?

regards,

david

Message was edited by:

davidosX

davidosXa at 2007-7-13 0:43:49 > top of Java-index,Desktop,Core GUI APIs...