Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 825
John Vu Avatar asked May 06 '12 04:05

John Vu


People also ask

How do you make a menu appear in Swing?

In order to create menu items in Swing, you need to create new instances of JMenuItem and set different properties for them. You can create menu item with both text and icon. Creates a JMenuItem instance without icon or text. Creates a JMenuItem instance with a given icon.

Is used to create menu bar in Swing?

JMenuBar, JMenu and JMenuItems are a part of Java Swing package. JMenuBar is an implementation of menu bar . the JMenuBar contains one or more JMenu objects, when the JMenu objects are selected they display a popup showing one or more JMenuItems .


1 Answers

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/

like image 99
COD3BOY Avatar answered Oct 06 '22 00:10

COD3BOY