JTable refresh

U

Urraco

"The trouble is, the JTable is only displaying the first element in
rowData."


I'm having trouble displaying all the information in a JTable. First I
create two vectors:
rowData = new Vector<Vector<String>>( ); //info obtained
from DataBase
colNames = new Vector<String>( );
I fill colNames with column name. Then I fill rowData depending on the
selection from a JComboBox using this method:

private void populateRowData( ) {
rowData.clear();
System.out.println("After Clear " + rowData); //for
debug
try {
rs.beforeFirst( ); // rs RecordSet
int position = account.getSelectedIndex(); //
account - JComboBox

while(rs.next( )) {
if (
rs.getString(3).equals(accountName.elementAt(position).elementAt(0)) &&
rs.getString(2).equals(accountName.elementAt(position).elementAt(1)) )
{
Vector<String> temp = new
Vector<String>( );
temp.add(rs.getString(1));
//date
temp.add(rs.getString(5));
//trans type
temp.add(rs.getString(6));
//trans amount
temp.add(rs.getString(7));
//running bal
rowData.add(temp);
}
}
System.out.println("After While " + rowData); //
for debug
}
catch(SQLException sqle) {
System.err.println(sqle);
}
}

finally I create a JTable:

table = new JTable(rowData, colNames);
sp = new JScrollPane(table);

The trouble is, the JTable is only displaying the first element in
rowData. If I print rowData to the black window I can see several
elements. The JTable does change with a different selection on the
JComboBox but for some reason only the first element in rowData is
displayed even if there are several elements in the Vector.

I've thought about using some kind of TableModel and changing the code
to this:

TableModel model = new AbstractTableModel( );
table = new JTable(model);
sp = new JScrollPane(table);

But how do I feed the two Vectors to the model and how do I update it
later? I'm just not sure this will help me as I don't really know
where my problem lies.

Any help or advice would be appreciated.
Urraco

java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)
 
V

Vova Reznik

Urraco said:
"The trouble is, the JTable is only displaying the first element in
rowData."


I'm having trouble displaying all the information in a JTable. First I
create two vectors:
rowData = new Vector<Vector<String>>( ); //info obtained
from DataBase
colNames = new Vector<String>( );
I fill colNames with column name. Then I fill rowData depending on the
selection from a JComboBox using this method:

private void populateRowData( ) {
rowData.clear();
System.out.println("After Clear " + rowData); //for
debug
try {
rs.beforeFirst( ); // rs RecordSet
int position = account.getSelectedIndex(); //
account - JComboBox

while(rs.next( )) {
if (
rs.getString(3).equals(accountName.elementAt(position).elementAt(0)) &&
rs.getString(2).equals(accountName.elementAt(position).elementAt(1)) )
{
Vector<String> temp = new
Vector<String>( );
temp.add(rs.getString(1));
//date
temp.add(rs.getString(5));
//trans type
temp.add(rs.getString(6));
//trans amount
temp.add(rs.getString(7));
//running bal
rowData.add(temp);
}
}
System.out.println("After While " + rowData); //
for debug
}
catch(SQLException sqle) {
System.err.println(sqle);
}
}

finally I create a JTable:

table = new JTable(rowData, colNames);

JTable automatically creates model as DefaultTableModel

You may replace it with
DefaultTableModel model = new DefaultTableModel(rowData, colNames);
table = new JTable(model);
sp = new JScrollPane(table);

The trouble is, the JTable is only displaying the first element in
rowData. If I print rowData to the black window I can see several
elements. The JTable does change with a different selection on the
JComboBox but for some reason only the first element in rowData is
displayed even if there are several elements in the Vector.

I've thought about using some kind of TableModel and changing the code
to this:

TableModel model = new AbstractTableModel( );

Impossible!!! AbstractTableModel is ABSTRACT
table = new JTable(model);
sp = new JScrollPane(table);

But how do I feed the two Vectors to the model and how do I update it
later? I'm just not sure this will help me as I don't really know
where my problem lies.

Create your own model subclassing AbstractTableModel or use
DefaultTableModel that designed for keeping data as Vector of Vectors
 
U

Urraco

Thanks for your help. Although the problem lied not in my lack of using
a TableModel of sorts, but the way I was trying to update the table.
My table is not being edited so a TableModel doesn't help me. Only the
data model was being updated. When I called table.revalidate( ) at an
earlier point, I was receiving a null error. It was true the table
wasn't created at the point yet. I just didn't see it with the way I
had my classes separated. In the end, table.revalidate( ) did the trick
and my problem is solved.

Looking back, the table should have updated itself when the data
changed. However, I think that using a for loop and updating the data
model repeatedly in quick succession, the JVM couldn't keep up with the
calls to update the table. After all why would the JTable only display
the first element in the data model when there were many others? That
would be my best guess. Any others?

Thanks again
Urraco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top