Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Using JPanel to create JTabbedPane

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

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 ]
 
Brian Cole
Author
Posts: 974
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
You showed up just in time for the waffles! And this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic