• Post Reply Bookmark Topic Watch Topic
  • New Topic

i need space between menus

 
sarim raza
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My menus are attached to each other, and displayed horizontally, i need space between them .
Here is my code:
I used addSeparator(), but I think that is to create space between menuitems and not between menus ....
//Create the menu bar.
menubar = new JMenuBar();
//Build the first menu.
menu1 = new JMenu("Options");
menu1.setBackground(color3);
menu1.addSeparator();
menu2 = new JMenu("Help");
menu2.setBackground(color3);
menu2.addSeparator();
menu3 = new JMenu("Edit");
menu3.setBackground(color3);
menu3.addSeparator();
menu4 = new JMenu("Quit");
menu4.setBackground(color3);
menu4.addSeparator();

menubar.add(menu1);
menubar.add(menu2);
menubar.add(menu3);
menubar.add(menu4);
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sarim,
I never found any documentation on this and worked on it for several days until I just decided to try something out of desparation and it worked. Just add an empty JPanel to the menu bar and it will add space.
For example:

Hope this helps,
Michael Morris
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just out of curiosity, why do you need space between them?
 
Greg Niessen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JMenu chartOptions = new JMenu("<html><font face=Georgia, Arial color=black><b>"
+ "Chart Options</b></font></html>");
chartOptions.setMargin(new Insets(0,50,0,50));
bar.add(chartOptions);

You can add the insets to the bar itself, but then it makes the menu bar larger. Insets are same as GridBag Layout. First Integer is number of pixels from the top, the second is from the left, the third is from the bottom, and the fourth from the right.

Hope this helps.
 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Greg, and welcome to JavaRanch! It's always good to see another 2 G Greg around here!

This particular question was asked over ten years ago, so the OP has most likely gone on to other problems by now. Stick around though! There are plenty of questions of more recent vintage that need answering.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!