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

Layout Problems

Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
With the following code, I get two layout problems. One, my menubar does not stretch all the way to each side of the JFrame. This in turn seems to be causing my TabbedPanes to line up incorrectly, with there only being room for two TabbedPanes per row. I would like all 5 to be in one row? What is causing this?

Ignore makeConversionsLabel, for it was not finished at the time of posting.

Thanks in advance for your help.
Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
Ah ha! I fixed the TabbedPane problem, but the menu bar still does not reach from side to side, how do I fix this?

EDIT: Changed screen to screen to, side to side.
[ October 15, 2005: Message edited by: Atrus Greyor ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
change this
//panel.add(new addMenu(), BorderLayout.NORTH);
to this
frame.setJMenuBar(new addMenu());

here's the new addMenu() class
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I came up with a slightly different approach (before seeing Michael's post) and a few other comments:
JMenuBar extends from JComponent so you can add it to a Container just like any other JComponent. When you add the JMenuBar to a JPanel (which has FlowLayout as its default layout manager) it will be centered and shown at its preferred size. When you add the JPanel to the north section of a BorderLayout the panel will expand horizontally to fill the width of the north section allowing the JMenuBar to remain at its preferred size.

You can add the JMenuBar to the JFrame with the 'setJMenuBar' method. Or you could add the JMenuBar to the north section of the JFrames default BorderLayout which will expand it (the menuBar) to fill the north section (which the menuBars parent JPanel was doing before).

Here I used the 'setJMenuBar' method, eliminated the JPanel extension from the (CustomMenu) class that makes the menus and menuBar and added a 'get' method to return the JMenuBar.

About the JFrame: you can extend JFrame and call JFrame methods on it from within the class. Or you can not extend JFrame, create a new JFrame and call JFrame methods on it, ie the reference to the new JFrame (the variable 'frame' in your code). These are two ways of making a JFrame for your gui so choose one or the other but not both. The first uses inheritance. The second uses composition.
Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
Thank you, thank you, it looks fine now!
I agree. Here's the link:
subject: Layout Problems
It's not a secret anymore!