• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Layout Problems

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, thank you, it looks fine now!
 
reply
    Bookmark Topic Watch Topic
  • New Topic