File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Populate JTable With Database Data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Populate JTable With Database Data" Watch "Populate JTable With Database Data" New topic

Populate JTable With Database Data

Joe Mastro

Joined: Jan 05, 2012
Posts: 14
I am trying to populate a JTable from a jButton event with data that I'm retrieving from an sqlite db. I have no trouble connecting to database and populating the 'data' and 'row' vectors but when I try to pass them to my JTable (jtblReqList) it does not seem to populate. I am under the impression that I should be able to popluate the JTable with the data contained in the vectors without using any of the table models. Any help is appreciated....thanks.

Paul Clapham

Joined: Oct 14, 2005
Posts: 19869

You're creating a JTable, but I don't see what that has to do with your GUI. I don't see any code where you add that JTable to any GUI.

Typically what you do is to create a GUI, including a JTable. Then when you get the data from the DB you put it into a DataModel, then update the existing JTable by calling its setModel() method (warning... name of method pulled from unreliable memory). Creating a new JTable is generally pointless as it isn't part of the GUI. Trying to get it into the GUI is also difficult to do, but since setting the data model of the one which is already there works, you don't need to futz with that either.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20193

Welcome to the Ranch, Joe!
Can you please UseCodeTags from now on? I've added them for you, and you can see how much easier it is to read your code.

How To Ask Questions How To Answer Questions
Nitin Surana
Ranch Hand

Joined: Jan 21, 2011
Posts: 129

May be this link will help you.
Joe Mastro

Joined: Jan 05, 2012
Posts: 14
Awesome, thanks everyone. As Paul stated there was no reason to create a new JTable when I already had one existing on my JFrame. I used the DefaultTable model in Nitin's link and set my JTable (jtblReqList) to the model and this at the very least populated the JTable.

I agree. Here's the link:
subject: Populate JTable With Database Data
It's not a secret anymore!