• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Justifying other components in a JMenuBar

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to add a component other than a JMenu to a JMenuBar. In particular, I'm adding a label that displays the current date and time. I can add the label fine but it gets added and is located right next to the last JMenu that I added. I'd really like it to be right-justified (while all the JMenus are left-justified). I thought maybe by adding the label via a

call would work but this didn't do anything. Is there any way to force the justification of a component in a JMenuBar?

Thanks,
Dave
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This doesn't do anything because the JMenuBar doesn't use a BorderLayout.

(And, if you think about it, BorderLayout wouldn't work for a JMenuBar anyway...)

You *can* set a different layout manager for your JMenuBar - but this is probably overkill for what you're trying to do.

All you need to do is add a Box.createHorizontalGlue() to the JMenuBar right before you add the component that you want to appear on the right hand side. The "Glue" will take up the additional space in the menu bar and push the next component to the right side.
 
David Irwin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nathan,

Thanks for your response. That worked great!

Dave
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic