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 Using JPanel to create JTabbedPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using JPanel to create JTabbedPane" Watch "Using JPanel to create JTabbedPane" New topic

Using JPanel to create JTabbedPane

Martin Grosse

Joined: Jan 31, 2008
Posts: 1
Hello. Let me first say that im still a beginner at java and i've been trying to hammer some code together to make a frame with a menu and within this menu i want to be able to press "new" and a new tab will come up..

This is the code (there might be easier ways to do it but this is the way i've come up with) anyhow.. here is my problem.. if you run the code you see a frame coming up with a white background.. all nice and fine.. however when i press file and new i get the tab "behind" the jframe (if you resize the window and make it larger you'll see the tabbedpane behind the white background. now if you rerun the code and minimize the window so that you only see the menu and then press new and resize the window it works fine.. this made me to come to the conclusion that the frame isnt updated when i press new.. i might be wrong but this is what i think is wrong..
So anyhow anyone got a sollution to this?
Any help is appretiated
// Martin
Brian Cole
Ranch Hand

Joined: Sep 20, 2005
Posts: 878
Originally posted by Martin Grosse:

There's a bunch of odd stuff in your code, but let me focus on this ActionListener.

1) The frame (actually its content pane) already has a component in CENTER. The first time the menu item is selected it is a JScrollPane with a JTextArea in it. Subsequent times it will be the instance of your 'window' class created the previous time. Is it your intent to keep replacing the CENTER component?

2) If so, you are correct to call validate() but you are calling it on the wrong object. You should be calling it on the container to which things have been added. So in this case: frame.getContentPane().validate(); It probably should be followed with frame.getContentPane().repaint(). [Also, there's no point in calling setVisible(true) here.]

But I don't think this is really what you want to do. It sounds like you would be better off adding a single JTabbedPane (initially with no tabs) at the start, then adding a tab to it each time the user selects the menu item. For this you shouldn't have to mess with any validate/revalidate/repaint/setVisible stuff.

While I'm here, some further comments:

A) The convention for class names (including nested class names) is they should start with a capital letter. It's not a big deal, but it usually makes sense to follow convention.

B) What is the point of class menu and class run? It seems to me all the methods and fields of those nested classes could just as well be in your top-level window class.

C) It seems strange to me that frame is a static field. In fact, I would be tempted to remove almost every occurrence of the keyword static in your entire program. (Why? Because then the no-longer-static code, such as your ActionListener above, could refer any member fields of your window class. One such field, I would think, would be the JTabbedPane you want your ActionListener to add tabs to.)

The main() method should still be public static void, of course. But since createAndShowGUI() would no longer be static, main() would have to replace window.createAndShowGUI() with
new window().createAndShowGUI(). [The makeTextPanel() method could also remain static if you wish.]

I think with these suggestions you could simplify your code quite a bit.

[edit: added the (Why? ...) motivation for removing the statics]
[ February 01, 2008: Message edited by: Brian Cole ]

bitguru blog
Brian Cole
Ranch Hand

Joined: Sep 20, 2005
Posts: 878
Has there been a change to the code JavaRanch uses for editing posts?

I ask because now when I attempt to edit a post it seems to append a duplicate post.

[edit: At least I can delete the duplicates afterward.]
[ February 01, 2008: Message edited by: Brian Cole ]
I agree. Here's the link:
subject: Using JPanel to create JTabbedPane
It's not a secret anymore!