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
Joined: Sep 20, 2005
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 ]