aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Jtable problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Jtable problem" Watch "Jtable problem" New topic
Author

Jtable problem

Aidan Cowley
Greenhorn

Joined: Feb 18, 2002
Posts: 4
Hey all,
I have a serious problem with getting my Jtable to display. It only displays the column names - not the data. The data is being derived from a result set (JDBC) - I know that that, at least, is working fine.
The Jtable uses the DefaultTableModel, and is displayed inside an internal window of the application.
Heres the relevant code:
public class SQL_Statements
{
public Vector getDescription()
{
try
{
SQL = "SELECT Description FRO Job_In_Info";
query_results = s.executeQuery(SQL);
while(query_results.next())
{
v.add(query_results.getString(1));
}
}
catch(SQLException e)
{
System.err.println("SQL Exception: " + e);
System.exit(0);
}
return v;
}
//The getDescription code works fine , i.e. System.out.println(v.lastElement()); produces whats it's meant to produce!
....
....
The main code is here:
SQL_Statements SQL;
JInternalFrame in = new JInternalFrame();
//Internal window named IN
Container contentPane = in.getContentPane();
DefaultTableModel defaultModel = new DefaultTableModel();
defaultModel.addColumn("Description", SQL.getDescription());
//addColumn susposedly has the following parameters addColumn(object column name, vector data);
JTable table = new JTable(defaultModel);
JScrollPane scroller = new JScrollPane(table);
contentPane.add(scroller);
//Note: The program uses internal windows. Could that be part of the problem?

...
...
Thast all the Jtable relevant code. Any ideas why the column name but not the data are displaying?
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Aidan,
I think you are getting bitten by the old reference usage. The way a vector works is not how many people would expect regarding the resultset transfer. The vector actually only holds pointers to the resultset. What that means is that when you exit your getDescription method the resultset is being released and your vector is pointing to nothing.
I have had similar problems and the easy fix is the create a new object and place that into your vector. For example:
v.add( new String( query_results.getString(1) ) );
and I think you should then be good to go.
Regards,
Manfred.
Aidan Cowley
Greenhorn

Joined: Feb 18, 2002
Posts: 4
Thanks Manfred,
I can see what your saying . But I tried what you suggested and it still doesn't work. Perhaps vectors aren't the best way to handle this?
Any other ideas?
Aidan
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Aidan,
I assume that you have checked:
Vector v = SQL.getDesriptions();
System.out.println( v );
and it works? Have you tried just placing some strings into a vector and seeing if the table shows them.
I don't really know what else to do ... the type of classes you have used should work because I do it all the time with my Db servlets and applets.
Regards,
Manfred.
Aidan Cowley
Greenhorn

Joined: Feb 18, 2002
Posts: 4
Hey again,
I ran that code with a whole bunch of println's just to see if it works. Alas, the method seems fine; i get the desired output on the console. For some truly mind boggling reason, it refuses to output it to the Jtable on the GUI - only the column names!
Vector v = new Vector();
v = SQL.getDescription();
System.out.println(v);
Then I immediately output it as follows:
defaultModel.addColumn("Description", v);
I get the "Description" table header - thats it though.
Just out of last ditch thoughts, I don't have to declare the table visible or something?

Thanks a million for the help,
Aidan
Aidan Cowley
Greenhorn

Joined: Feb 18, 2002
Posts: 4
Oh, just found out that this does NOT print to the Jtable:
Vector v = new Vector();
v.add("Aidan");
System.out.println(v);
DefaultTableModel defaultModel = new DefaultTableModel();
efaultModel.addColumn("Description", v);
 
wood burning stoves
 
subject: Jtable problem