Question

I have created a complete menu bar for my java program and it is all functioning correctly, however I would like to have an image for the file menu, instead of text. I know you can add and icon or image for JMenuItem's but is it possible to do it for the JMenu itself.

So instead of text where it says "File" or "Edit" have images there so that when you click the image it displays the drop down/JMenuItem's?

A short snippet from my code is below

    //File Menu.
    JLabel fileButton = new JLabel(new ImageIcon("buttonfile.jpg"));
    MenuBarFile = new JMenu("file");
    //MenuBarFile.setIcon("buttonfile.jpg");
    menuBar.add(menu);

The class extends JMenuBar by the way, and is added into another class where needed.

By the way I have tried the obvious which is:

menuBar.add(fileButton);

Adding a JLabel image to the menuBar doesn't do anything, it just displays blank.

Was it helpful?

Solution

JMenu inherits from AbstractButton, which has a setIcon() method allowing to set the icon of the menu.

BTW, JMenu also inherits from JMenuItem. So if you managed to set an icon on a JMenuItem, you should be able to do it, the same way, on a JMenu.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top