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 ]