aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JMenuBar not using default alignment? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JMenuBar not using default alignment?" Watch "JMenuBar not using default alignment?" New topic
Author

JMenuBar not using default alignment?

Phil Freihofner
Ranch Hand

Joined: Sep 01, 2010
Posts: 115
    
    1
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
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2203
    
    7
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

Joined: Sep 01, 2010
Posts: 115
    
    1
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JMenuBar not using default alignment?