aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Table Viewer GUI from a Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Table Viewer GUI from a Database" Watch "Table Viewer GUI from a Database" New topic
Author

Table Viewer GUI from a Database

Mitul Patell
Greenhorn

Joined: Dec 17, 2012
Posts: 17
Hello all,

I'm afraid I'm quite the Java beginner so please bear with me.

I've created a database containing eight separate tables with varying amounts of columns. I'm attempting to make a GUI that will let me pick specific columns out of a table and display a JTable of just those columns. So far all I've accomplished is getting the database to connect properly and I've created a JComboBox that holds the names of all of the tables within the database. My next step is getting a JList to fill itself with the columns of the selected Table from the JComboBox. I have absolutely no idea how to go about doing this.

My current code is as follows:



Now I have tried various ways to get the JList to populate itself with the column names but I can't seem to get it. I think I somehow have to use this:



but I'm not really sure.

Any tips would be much appreciated. If I was unclear about anything, please feel free to ask.
Mitul Patell
Greenhorn

Joined: Dec 17, 2012
Posts: 17
Sorry for the double post. At first it told me it didn't go through. Not sure how to delete my post.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

The second part of the code can be a method, where you can pass the selected table name, and on combo box change event, you can call that method with the selected table name.





Swastik
Mitul Patell
Greenhorn

Joined: Dec 17, 2012
Posts: 17
Swastik Dey wrote:The second part of the code can be a method, where you can pass the selected table name, and on combo box change event, you can call that method with the selected table name.





This is precisely what I tried to do a little while ago, however I am unsure of how/where I would then implement the line.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

May be in the fetchColumns method, after the for loop ends.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4544
    
    5

Mitul Patell wrote:Sorry for the double post. At first it told me it didn't go through. Not sure how to delete my post.


No worries. I've removed it.


luck, db
There are no new questions, but there may be new answers.
Mitul Patell
Greenhorn

Joined: Dec 17, 2012
Posts: 17
Swastik Dey wrote:May be in the fetchColumns method, after the for loop ends.


The problem with that is I can not add that JList to the panel/frame in that case because it is in a separate method. Or maybe there is a way to do that?

EDIT: Nevermind. Mind blanked for a second.
Mitul Patell
Greenhorn

Joined: Dec 17, 2012
Posts: 17
Darryl Burke wrote:
Mitul Patell wrote:Sorry for the double post. At first it told me it didn't go through. Not sure how to delete my post.


No worries. I've removed it.


Thanks.
Mitul Patell
Greenhorn

Joined: Dec 17, 2012
Posts: 17
When I do add the jlstColNames after the for loop, I get a rather long error. I won't post the entire thing here unless someone asks for it, but the main issue seems to be here:

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1031)
at java.awt.Container.add(Container.java:352)
at TableViewer2.<init>(TableViewer2.java:55)
at TableViewer2.main(TableViewer2.java:75)

Is that a normal problem that anyone else has run into?

EDIT:

Line 55:

Line 75:

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Use the no-arg constructor to create Jlist object, and add it to JPanel in TableViewer2(). In fetch columns method use setListData method to set data to list. As far as the error is concerned, the error is something related to the database.
Mitul Patell
Greenhorn

Joined: Dec 17, 2012
Posts: 17
Swastik Dey wrote:Use the no-arg constructor to create Jlist object, and add it to JPanel in TableViewer2(). In fetch columns method use setListData method to set data to list. As far as the error is concerned, the error is something related to the database.


Yeah I apologize some stupid mistakes by me. I edited that last post for the updated error message right before you posted this. With or without the no-arg constructor and setListData method I am getting the same error.
Mitul Patell
Greenhorn

Joined: Dec 17, 2012
Posts: 17
Just don't understand. It's saying that jlstColNames has a null value?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Could you please post the updated code?
Mitul Patell
Greenhorn

Joined: Dec 17, 2012
Posts: 17
Swastik Dey wrote:Could you please post the updated code?


Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Try this
Mitul Patell
Greenhorn

Joined: Dec 17, 2012
Posts: 17
Swastik Dey wrote:Try this
...
...


Precisely the same error.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Which line is throwing npe? Show the exception stack trace.
Mitul Patell
Greenhorn

Joined: Dec 17, 2012
Posts: 17
Swastik Dey wrote:Which line is throwing npe? Show the exception stack trace.


Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1031)
at java.awt.Container.add(Container.java:352)
at TableViewer2.<init>(TableViewer2.java:54)
at TableViewer2.main(TableViewer2.java:74)
Java Result: 1

Line 54:
Line 74:
Mitul Patell
Greenhorn

Joined: Dec 17, 2012
Posts: 17
Guess you're out of ideas too.

Well thanks for your time Swastik
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

It's not that hard. When line 54 is called, the variable jlstColNames is null. You can't add null to a panel, that doesn't make sense. So either remove that line of code or put something before it which assigns a reference to some component to the variable.
Mitul Patell
Greenhorn

Joined: Dec 17, 2012
Posts: 17
Paul Clapham wrote:It's not that hard. When line 54 is called, the variable jlstColNames is null. You can't add null to a panel, that doesn't make sense. So either remove that line of code or put something before it which assigns a reference to some component to the variable.


I created the JList before adding it to the panel and just setListData in the fetchColumns method. No NullPoint error, but the JList isn't showing up at all. Makes no sense. Any idea why that's happening?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4544
    
    5

Mitul Patell wrote:I created the JList before adding it to the panel

Not before the first time you tried to add it to the panel, you didn't.
Mitul Patell
Greenhorn

Joined: Dec 17, 2012
Posts: 17
Darryl Burke wrote:
Mitul Patell wrote:I created the JList before adding it to the panel

Not before the first time you tried to add it to the panel, you didn't.


Oh sorry the code above is not updated. This is the updated code:



And now with this code I'm not getting any errors, but for some reason the JList is not showing up on the Frame at all.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4544
    
    5

How many rows is the JList populated with when it is *not* displaying?

Note that the usual way to display a JList is to first wrap it in a JScrollPane.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Table Viewer GUI from a Database