Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Layout Problems

 
Atrus Greyor
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
change this
//panel.add(new addMenu(), BorderLayout.NORTH);
to this
frame.setJMenuBar(new addMenu());

here's the new addMenu() class
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, thank you, it looks fine now!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic