• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JMenuBar not using default alignment?

 
Ranch Hand
Posts: 140
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi -

I added a JMenuBar to a subclassed JPanel. The menu bar appears in the top center. I'd prefer to have it appear in the top left, but am unable as yet to figure out how to do this. According to the Java Tutorials, JMenuBar uses a BoxLayout? I've tried using the .setAlignment(LEFT_ALIGNMENT) method on the JMenuBar, but this doesn't change the position of the JMenuBar on the JPanel.

Previously, I had placed the JMenuBar in a JFrame, as I only had a single JPanel of content on that frame, and there the constituent JMenus appear in the Top Left corner of the screen, which is what I want to see. But now, I have additional JPanels of content, and each has its own menu needs, which is why I'm trying to play the MenuBars on the JPanels instead of the JFrame.

I have tried adding a "dummy" JMenu to the JMenuBar, but this 2nd simply appears on the right of the first JMenu (as expected) with both in the top center. I tried putting a JMenu of "Box.HORIZONTAL_GLUE" to separate the two JMenus, in hopes of splitting them apart to either corner, but the two JMenus remain contiguous!?

Here is a slightly simplified listing.



Is putting Menu bars on JPanels not a good idea? As I mentioned earlier, my app now uses multiple JPanels of content, and allows navigation between them, and each has its own menu functions. If I put the JMenuBar in the JFrame, I will have to figure out how to alter the contents of the JMenuBar depending upon which JPanel is current. I thought I'd check in as see if there was something I missed (concerning the positioning of JMenuBars on JPanels) before resorting to that.

Ah, also: the subclassed JPanel was left with its default layout. That would be a Flow layout, flowing from left to right, yes? Is a JMenuBar even subject to this layout, since it is something that usually hangs from the top of the container, not an item being presented at any point within the container? The other objects on my JPanels are all using absolute positioning.

Many thanks!

Phil Freihofner
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

According to the Java Tutorials, JMenuBar uses a BoxLayout?



Yes, the menus added to the menu bar are layed out using the BoxLayout.

But you added the menubar to a JPanel which uses a FlowLayout and the default for a FlowLayout is to align the components in the center. You can change the FlowLayout to use left alignment.

> But now, I have additional JPanels of content, and each has its own menu needs, which is why I'm trying to play the MenuBars on the JPanels instead of the JFrame.

You can reset the menu bar every time you change the panel.

Or using a panel with a BorderLayout. Add the menu bar to the NORTH and your other panel to the CENTER.
 
Phil Freihofner
Ranch Hand
Posts: 140
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given your suggestion about the default operation of a flow layout, I added this line:


and the JMenuBar now starts from the top left corner. You've saved me a lot of work!

Thank you,

Phil F.
 
I can't take it! You are too smart for me! Here is the tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic