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

