aspose file tools*
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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Populate JTable With Database Data" Watch "Populate JTable With Database Data" New topic
Author

Populate JTable With Database Data

Joe Mastro
Greenhorn

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
Bartender

Joined: Oct 14, 2005
Posts: 18650
    
    8

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
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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.

http://www.onlinexamples.com/showfullexample.action?idexamples=76&title=Update%20Jtable%20Dynamically%20In%20The%20Gui%20Say%20For%20E.g.%20In%20A%20Jframe
Joe Mastro
Greenhorn

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: http://aspose.com/file-tools
 
subject: Populate JTable With Database Data