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 Trouble updating JComboBox on JTabbedPane event click Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Trouble updating JComboBox on JTabbedPane event click" Watch "Trouble updating JComboBox on JTabbedPane event click" New topic

Trouble updating JComboBox on JTabbedPane event click

Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
Hi All,

I've got another puzzle for you...

I've created a small swing application which has 2 tabs.
the contents of each tab is created in a seperate class returning a JPanel.

The 2nd tab is where the user can edit, delete, or create new files.
When either a new file is created or an existing one is deleted an array is updated containing
the names of remaining files.

The 1st Tab is where the user would be able to read files and compare files with other files.
So on the 1st Tab there is a JComboBox which contains the list of files.

In my frame class is where I implement the JTabbedPane for the two tabs.
Here is where I have the changeListener located.

Here's the code where I'm having the issue:
Seems every time I try this I get a NullPointerException where indicated below in the code.

I also tried to putting the body of code I have within the ChangeListener above into its own
method of the 1st Tab class and call it from the ChangeListener of the frame class and also
get a NullPointerException in the same place.

Let me know if if you have any ideas on this, I've scoured oracles site and countless others with no luck
and have worked on solving this on my own for two days now, feeling quite frustrated at this point.

Any help would be greatly appreciated.
Paul Clapham

Joined: Oct 14, 2005
Posts: 18541

Well, that would be because mp.select_cmb is null. What else could it be -- mp certainly can't be null at that point.

But I don't understand why you're creating a new MainPanel in that code. If I understand your design correctly, you already have a MainPanel displayed and you want to be working with that MainPanel.
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 169

I think you will get no error if you used your existing MainPanel. (I think you mentioned as my Panel to MainPanel)

Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
Hi Paul,

Thanks for checking this out.
There are four classes involved in this.
My entire application has 13 classes.
The code for the ChangeListener is located in the class I have my main method in.
The class MainPanel class is a seperate class that holds several methods which build the Panels for the first tab.
and The AddEditFormatter class is a seperate class that holds several methods which build the Panels for the second tab.

I hope that helps in understanding the local issue I presented.

I've verified that the JComboBox (select_cmb) located in the MainPanel class is not null.
But I still get a NullPointerException when I try to access it.

I've done this before without issue back in Java SE version 1.4, can't seem to figure out what's changed in verion 6
and what's replaced this usage. I've read over the documentation several times of what I could find, just haven't
found anything other than old documentation saying this method of doing this won't be supported after version 1.4.2.

There's got to be another way that exists to do the same thing.

Any help would be appreciated.

Joe McTigue
Ranch Hand

Joined: Mar 05, 2012
Posts: 52
I got this one figured out. :-)

I changed my code to the following:

Then from my MainPanel class I created the updateComboboxSelections() method as follows:

I actually tried this same approach in my first attempt and failed.
It seems the only thing different was my making mp a class varible.

consider this issue solved.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Trouble updating JComboBox on JTabbedPane event click
Similar Threads
Create panel depending on which tab is clicked in Swing
Can I use repaint() on a JFrame?
JTabbedPane with Keyevents
Swing ChangeEvent occur twice