2

I can only set horizontal separator to my code , how to set vertical one ? Similar to this http://jade-cheng.com/hpu/2012-spring/csci-2912/assignment-5/blueprint-2.png

    file.add(newMenuItem);
    file.add(openMenuItem);
    file.add(saveMenuItem);
    file.add(subMenu);
    file.addSeparator();
    file.add(exitMenuItem);
mussdroid
  • 732
  • 1
  • 9
  • 22
  • 1
    Can you post an [MCVE](http://stackoverflow.com/help/mcve) that demonstrates the problem? Also, a screenshot of what's happening and a mockup of what you want to happen might help as well. – Kevin Workman Mar 19 '14 at 14:05
  • 2
    And, could you whip up an ascii-art sketch of what you're trying to get? Why do you need a vertical separator in a menu? – splungebob Mar 19 '14 at 14:27
  • A vertical separator would be used wen components are displayed horizontally. A horizontal separator is used when components are displayed vertically. This is how all applications that use menus work, like your Browser, Word, etc. Companies spend millions of dollars to have a consistent UI. Why are you trying to change this? – camickr Mar 19 '14 at 14:52
  • I cannot imaging a vertical separators in MenuItem... Could you give some example? Some screen with expected result you've seen elsewhere? – radekEm Mar 19 '14 at 15:43
  • see vertical separator which contains icons guys http://www.java2s.com/Code/Java/GWT/MenuitemseparatorSmartGWT.htm – mussdroid Mar 19 '14 at 15:44
  • see here as well http://jade-cheng.com/hpu/2012-spring/csci-2912/assignment-5/blueprint-2.png – mussdroid Mar 19 '14 at 15:48
  • 3
    That sort of design decision would be a matter for the look and feel, you just set the icon for the menu item and let the L&F take care of rendering it in a way that's consistent with other apps on the target platform. – Ian Roberts Mar 19 '14 at 15:51

3 Answers3

3

Vertical separator in JMenuItem? The only thing which comes to my mind and which you can treat as a JSeparator is something like below:

enter image description here

But this left "JSeparator" is not an extra added JSeparator, but depends on LookAndFeel.

Below you see the same JFrame with the same JMenuBar but with different lookandfeel:

enter image description here

The code for both screens is exactly the same, but executed with different look and feels:

public class NewClass extends JFrame {

    public NewClass() throws HeadlessException {
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("File");
        menuBar.add(menu);
        menu.add(new JMenuItem("Open..."));
        menu.add(new JMenuItem("Save"));
        menu.add(new JMenuItem("Save as..."));
        menu.addSeparator();
        menu.add(new JMenuItem("Delete"));

        setJMenuBar(menuBar);
        setSize(new Dimension(500,500));
        setVisible(true);
    }

    public static void main(String[] args) {
        try {
           //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
           //UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
           UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());      
           new NewClass();
        } catch (ClassNotFoundException ex) {
          Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
          Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
          Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
          Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }   
}

Note, that

menu.add(new JSeparator(JSeparator.VERTICAL));

will not generate any separator at all (you can try)

radekEm
  • 4,617
  • 6
  • 32
  • 45
0

As already pointed out by @guitar_freak, some LayoutManagers give you this effect for free, whereas others do not. If you wanted this effect for any LM, you'll have to roll up your sleeves a bit.

A JMenu is an AbstractButton that has no layout by default. When you add something to a JMenu, you're actually adding it to the menu's internal JPopupMenu, which has a DefaultMenuLayout (subclass of BoxLayout) as it's layout by default.

Things to try: write your own MenuItemUI to install on the JMenu, or subclass the JMenu to use a JPopupMenu with a different LayoutManager. I haven't tried either so I'm not sure which is correct.

Personally, I'd just leave it up to the L&F as @guitar_freak suggested. It seems to much work for too little gain, but ultimately, only you can decide that.

splungebob
  • 5,357
  • 2
  • 22
  • 45
-1

I think this is what you're looking for:

file.add(new JSeparator(SwingConstants.VERTICAL));

Nestor
  • 53
  • 1
  • 1
  • 5