Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

JTable refresh

Thread view: 
Urraco - 01 Feb 2006 02:07 GMT
"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)
Vova Reznik - 01 Feb 2006 14:43 GMT
> "The trouble is, the JTable is only displaying the first element in
> rowData."
[quoted text clipped - 45 lines]
>
>              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);
>
[quoted text clipped - 8 lines]
>
>              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

> 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)
Urraco - 01 Feb 2006 23:00 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.