aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Resize problem with JMenuBar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Resize problem with JMenuBar" Watch "Resize problem with JMenuBar" New topic
Author

Resize problem with JMenuBar

Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

I am working under Windows OS, the default behavior while resizing application under windows is to bring the menu downwards if it doesn't fit the size. (Please refer the attachment Expected.jpg) where i tried resizing the Eclipse IDE window which shows the menu perfectly and all the menu's are accessible

Wherein if i try to resize the JFrame which i have created using the code below



I can see that the menu is no longer usable (Please refer Result.jpg in attachment). How can i prevent this from happening. I tried using FlowLayout on JMenuBar but still i can't see any difference.



[Thumbnail for Result.jpg]


[Thumbnail for Expected.jpg]

Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Finally, searching the bug tracker of java revealed a similar issue which i am facing and i tried out the code provided in the bug tracker

Old Java Bug

and that one seemed to work if i have only the menu aligned in the left of the JFrame. When i tried adding a JLabel onto JMenuBar using Box.createHorizontalGlue() to align only the JLabel to the right the custom layout code is not working as it uses the FlowLayout wherein the Glue will work perfectly under BoxLayout only. So any idea how to over come this situation.

Here is the updated code which i am trying now
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2225
    
    8
Create a panel that contains 2 menubars. One that can wrap and one that can't.

Something like the following. This examples uses the Wrap Layout which probably does the same thing as your CustomJMenuBarLayout, but as the class name implies it is more general. Choose whichever layout you want to use.

Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Rob Camick - Thanks for your reply

I tried out your code and found that it is working perfectly fine with the JMenu Help laid at the right end of JMenuBar. But, when i tried to change your code to meet my requirement of placing a JLabel at the right end i end up with another problem.

I took your code and then replaced Help - JMenu with a JLabel



When i run this code initially it came up well (Attachment Step1.jpg) then i tried resizing to increase the size of JFrame which also worked perfectly (Attachment Step2.jpg) then i tried resizing to reducing the size of JFrame and found that the JLabel which was added is causing the JMenu to disappear(Attachment Step3.jpg)

I tried the same operation with native windows application which does a reverse case of hiding the Label. So can you tell me your suggestion to make the same for this JFrame as well. If i try to reduce the size of the JFrame then the JLabel can be hidden but the JMenu should appear.

And finally you got a nice blog post explaining the issue and can i use your WrapLayout.java in my code provided i will include your blog URL in the code.



[Thumbnail for Step1.jpg]


[Thumbnail for Step2.jpg]


[Thumbnail for Step3.jpg]

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2225
    
    8
Add a ComponentListener to the panel. On the componentResized() event you need to do a calculation that determines it the label should be visible or not based on the width of the panel the label and the menu items.
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Rob i will try out your suggestion.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Resize problem with JMenuBar