How to use Menu Separators in Java?
I have created a menu in Java. I want to group some of the options in a Menu, and i an unable to do it.!! This is much common in Menus. The options that are having similar purpose are grouped in one side. Like you can see on your computer menus. I think that somebody would have got my point .!! Please explain me how to use Menu Separators in Java.?? It would be grateful if someone can provide me the coding for the same.!
Re: How to use Menu Separators in Java?
You want the Menu Separator so that you can group the menus.?! This is also seen in Windows operating system. For that purpose you can use the JSeparator class. A horizontal or vertical dividing line or empty space is being provided by the JSeparator. As you said it is most commonly used in menus and tool bars. Separators are somewhat similar to borders even though they are genuine components. Hope you got some useful points from this.!!
Re: How to use Menu Separators in Java?
The code for the menu separators is very simple. If you want to add the menu items and separators to the menu, you can glance the following example, in which I explained the same :
Code:
menu.add(menuItem1);
menu.add(menuItem2);
menu.addSeparator();
menu.add(rbMenuItem1);
menu.add(rbMenuItem2);
menu.add(menuItem3);
menu.addSeparator();
menu.add(cbMenuItem1);
menu.add(cbMenuItem2);
menu.add(menuItem3);
menu.addSeparator();
menu.add(submenu);
Re: How to use Menu Separators in Java?
You can use the JSeparator class directly to provide a dividing line in any container. I think that using the separators is very easy task because it does not have API (mostly). You will have to set the preferred size or put it in under the control of a layout manager, so that you can view the separator. Otherwise, it is visible unless you set that. You can do that in layout manager like BorderLayout or BoxLayout. Because these layouts easily gets stretched to fill its available display area.
Re: How to use Menu Separators in Java?
You can also put the ListDemo with the Panel containing the vertical separator. You can have a look at the following code snippet that shows the same :
Code:
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane,
BoxLayout.LINE_AXIS));
buttonPane.add(fireButton);
buttonPane.add(Box.createHorizontalStrut(7));
buttonPane.add(new JSeparator(SwingConstants.VERTICAL));
buttonPane.add(Box.createHorizontalStrut(7));
buttonPane.add(employeeName);
buttonPane.add(hireButton);
buttonPane.setBorder(BorderFactory.createEmptyBorder(7,7,7,7));