views:

334

answers:

4

Hi

I was trying to make a JMenu behave like a JButton but I'm having some problems and hopefully someone here can help!

I've added a MenuListener to the JMenu item with this but I cant get the popup menu/focus to leave to enable me to properly click the JMenu repeated times to trigger this function and i was hoping someone could tell me what i'm doing wrong. Thanks.

public void menuSelected(MenuEvent e)
        {
            ... // do stuff here code
            JMenu source = (JMenu)e.getSource();
            source.setSelected(false);
            source.setPopupMenuVisible(false);

        }
A: 

This code sample runs in eclipse, Again concerned about how you are using it?

public class MyMenuFrame extends JFrame {


    public MyMenuFrame() throws HeadlessException {
        super("My Frame");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(400, 300);
        Container pane = this.getContentPane();
        pane.setLayout(new BorderLayout());
        pane.add(new JLabel("Hi there"), BorderLayout.PAGE_START);
        this.setVisible(true);
        JMenuBar menubar = new JMenuBar();
        JMenu menu = new JMenu("File");

        menu.addMenuListener(new MenuListener() {

            @Override
            public void menuSelected(MenuEvent e) {
                System.out.println("a");

            }

            @Override
            public void menuDeselected(MenuEvent e) {
                System.out.println("a");

            }

            @Override
            public void menuCanceled(MenuEvent e) {
                System.out.println("a");

            }
        });
        menubar.add(menu);
        this.setJMenuBar(menubar );
    }

    public static void main(String[] args) {
        new MyMenuFrame();
    }
}
Calm Storm
+1  A: 

Not completely sure what you're asking...

But JMenuBar inherits from Container - if you'd rather add a JButton to it than a JMenu you can simply call -

JMenuBar menuBar = ....
JButton myButton = ....
menuBar.add(myButton);
Nate
Ahhhh I didnt realise you could add a JButton to a JMenuBar... Thanks soo much
Kurru
A: 

It's very difficult to determine what you're trying to do here. But I don't think you are properly using JMenu.

A JMenu is the object that represents a Menu. It is separate from the menu bar (JMenuBar) and from the menu item (JMenuItem). A JMenuBar usually contains multiple JMenus (File, Edit, etc) which in turn contain multiple JMenuItems (New, Open, Close). The JMenuItems are what is clicked and "acts like a button" in the menu.

To get a menu item to act like a button, simply add it to the menu. For example:

JMenu fileMenu = new JMenu("File");
JMenuItem newChoice = new JMenuItem("New");
newChoice.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        newHasBeenSelected();
    }
});
fileMenu.add(newChoice);

If you're trying to create a pop-up menu, you need to use JPopupMenu instead of JMenu, and you don't need a JMenuBar. Here are the Java tutorials on menus: http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html

And here are the Java docs for JMenuBar, JMenu, JPopupMenu, and JMenuItem.

If you edit your question and give a more detailed explanation of what you're doing I might be able to give more specific help.

Daniel Bingham
A: 

Hi!

I know this is an old thread, but I think I might have a solution. I stumbled accross this problem in one of my apps, and found a workaround. Try using a JMenuItem instead of a JMenu. It will have the same L&F as a JMenu when you attach it to a JMenuBar. The only thing you have to do is set the size of your new "button", as your Layout Manager (even if you have not set one) will resize this component based on its own rules:

http://www.javaworld.com/javaworld/jw-09-2000/jw-0922-javatraps.html

The way to do it is found under that link (if you feel uncomfortable clicking on the link, google for "setsize doesnt work" - it will be the in the top ten results). If you do not set the size properly, your new "button" will fill up the remaining space of your JMenuBar.

try this code:

menuItem.setMinimumSize(someMenu.getSize());
menuItem.setPreferredSize(someMenu.getSize());
menuItem.setMaximumSize(someMenu.getSize());
menuItem.setActionCommand("ActionText");

setActionCommand() method will set an action command, so that when you click your new "button" this will be the action command passed by the action event argument to the action performed method, so that you can easily identify it:

public void actionPerformed(ActionEvent e) {
    System.out.println(e.getActionCommand());
}

Hope this helps!

noResets