How do I create this special menu bar with Java Swing?

John Vu picture John Vu · May 6, 2012 · Viewed 11.1k times · Source

I am trying to implement this type of menu using Swing. Is there any off-the-shelf solution (free and/or commercial) yet?

http://i.stack.imgur.com/iwtQf.png

Answer

COD3BOY picture COD3BOY · May 6, 2012

Assuming you want that image on the menu, why not something like this?

BufferedImage image = ImageIO.read(url);

yourJMenu.setHorizontalTextPosition(SwingConstants.CENTER);
yourJMenu.setVerticalTextPosition(SwingConstants.BOTTOM);

yourJMenu.setIcon(new ImageIcon(image));

EDIT : Seems you're asking to start from scratch.

Please refer to: How to Use Menus before reading this answer.


EDIT 2 : Here is an SSCCE,

import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingConstants;

public class MenuTest {

    public static void main(String[] argv) throws Exception {
        // Create the menu bar
        JMenuBar menuBar = new JMenuBar();

        String imageURL = "http://blogs.discovermagazine.com/" +
            "drone360/wp-content/themes/discoverblog/images/" +
            "gear_icon.png";

        // Create a menu
        JMenu menu = new JMenu("Menu");
        BufferedImage image = ImageIO.read(new URL(imageURL));
        menu.setHorizontalTextPosition(SwingConstants.CENTER);
        menu.setVerticalTextPosition(SwingConstants.BOTTOM);
        menu.setIcon(new ImageIcon(image));
        menuBar.add(menu);

        // Create a menu item
        JMenuItem item = new JMenuItem("Test Item");

        menu.add(item);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setJMenuBar(menuBar);
        frame.setSize(500, 550);
        frame.setVisible(true);
    }
}

Resource courtesy : http://pscode.org/media/