3

I'm trying to insert / display a vertical separator between the icon and the text of the JMenuItem components in my applications. I create a JMenuItem as follows (roughly):

JMenuItem cutMenuItem = new JMenuItem();
cutMenuItem.setName("cutMenuItem");
cutMenuItem.setRequestFocusEnabled(false);
cutMenuItem.setText("cut");
cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK));

When I look at the items in the menu, they appear as follows:

Current look of menu items

Interestingly enough, I noticed that the default appearance of JMenu components matches the look that I want:

enter image description here

Naturally, changing all my JMenuItem components to JMenu components is not an acceptable solution. How can I get the JMenuItem components in my application have a vertical separator / border between the icon and the text?

Does this hinge on L&F? For the record, I am on a Windows 7 machine. I have tried setting the LayoutManager on the JMenuItem objects to BorderLayout:

cutMenuItem.setLayout(new BorderLayout(5,0));

Expecting to see a horizontal gap between the icon and text, but that seemed to make no difference.

EDIT: Here's a very fundamental SSCCE

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.event.KeyEvent;

public class FakeApp {

    public static void main(String args[]) {
        JFrame frame = new JFrame();
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Menu");

        JMenuItem menuItem = new JMenuItem();
        menuItem.setName("cutMenuItem");
        menuItem.setRequestFocusEnabled(false);
        menuItem.setText("cut");
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.CTRL_MASK));
        menuItem.setIcon(UIManager.getIcon("OptionPane.errorIcon"));

        menu.add(menuItem);
        menuBar.add(menu);
        frame.getRootPane().setJMenuBar(menuBar);
        frame.add(new JPanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(300, 300));
        frame.pack();
        frame.setVisible(true);
    }

}
eternaln00b
  • 1,043
  • 9
  • 18
  • 3
    *"I create a JMenuItem as follows **(roughly)**:"* The devil is in the details. For better help sooner, post an [SSCCE](http://sscce.org/). *"I'm trying to insert / display a vertical separator between the icon and the text of the JMenuItem components in my applications."* Why, exactly? – Andrew Thompson Mar 16 '12 at 16:31
  • "why"? Because it looks better IMHO, and is consistent with other applications (that are built on Qt, not Java) used in-house. Also, while I can certainly provide an SSCCE, where would anyone else running this code source an icon image from? – eternaln00b Mar 16 '12 at 16:39
  • 3
    eeeeeeerggghh UIManager.getIcon("OptionPane.errorIcon") and another :-) – mKorbel Mar 16 '12 at 16:43
  • 1
    Euh, using a simple URL pointing to any icon image on the web would do the trick. Don't dismiss suggestion made by people trying to help you. – Guillaume Polet Mar 16 '12 at 16:44
  • I'm not being dismissive, it's a simple question. It's no fault of mine if you choose to get offended. – eternaln00b Mar 16 '12 at 16:45
  • 1
    @AndrewThompson I'm actually at work right now, so if you went to my house, you wouldn't find me. :P Sorry, that's lame, i'm just trying to lighten the mood. I'm sorry if I came across as dismissive, but that's not my intention. I was going to post this question and then run to lunch so I was in a bit of a hurry, which is why I asked about where to source an image from rather than read the link you sent me. Any other time, I would have. Again, my mistake. Apologies all round. – eternaln00b Mar 16 '12 at 16:57

1 Answers1

3

If you simply set the L&F to the system L&F on Windows 7, you have the desired effect:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Or do you want this on all platforms/L&F ?

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • Thanks. Yes, ideally, I would like this on all platforms. I am not explicitly setting the L&F in my application since I don't want to deviate from the system defined L&F. As a result, I can't depend on L&F for this. But your answer / suggestion is certainly appreciated. – eternaln00b Mar 16 '12 at 17:00
  • 1
    use custom [Look and Feel](http://stackoverflow.com/questions/3954616/look-and-feel-in-java), you have take Swing only as skeleton – mKorbel Mar 16 '12 at 17:06
  • Look and Feel is the right way to go I guess, but not at an application level. I probably just want to override MenuItemUI to do the right thing. Thanks for pointing me in the right direction. – eternaln00b Mar 16 '12 at 20:35