• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Table Viewer GUI from a Database

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the double post. At first it told me it didn't go through. Not sure how to delete my post.
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



 
Mitul Patell
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May be in the fetchColumns method, after the for loop ends.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Mitul Patell
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just don't understand. It's saying that jlstColNames has a null value?
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please post the updated code?
 
Mitul Patell
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swastik Dey wrote:Could you please post the updated code?



 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this
 
Mitul Patell
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swastik Dey wrote:Try this
...
...



Precisely the same error.
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which line is throwing npe? Show the exception stack trace.
 
Mitul Patell
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guess you're out of ideas too.

Well thanks for your time Swastik
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic