Giving JMenuItem's name to it's ActionListener
Asked Answered
V

2

2

How can I give my JMenuItems names that the ActionListener attached to them will look at?

I've got a menu system that's handled by a single ActionListener, and some items in those menus duplicate names. This isn't a problem on the user end, because it's obvious what does what; in fact, it would be more confusing if they had different names. However, at my end, I want to label each item uniquely.

The section that creates my items looks like this:

String label = getLabel(forThisItem);
JMenuItem item = new JMenuItem(label);
item.setName(parentMenu.getName() + "_" + label);
item.addActionListener(actionListener);
parentmenu.add(item);

Interrogating the item afterwards (and outside the scope of this method) with getName() gives the name I gave it, as it should, but the output of

public void actionPerformed(ActionEvent ae) {
    String actionPerformed = ae.getActionCommand();
    System.out.println("actionPerformed: " + actionPerformed);
}

is the the, possibly duplicated, name that the user sees, specified by label, not the unique name that I gave it.

How can I give the right information to the ActionListener?

Veranda answered 25/1, 2012 at 17:32 Comment(0)
A
4

Why aren't you calling setActionCommand on the menuitem. Instead of using setName, if you call setActionCommand, you should get what you expect when you call getActionCommand

Also, its label, not lable.

Amaryllis answered 25/1, 2012 at 17:44 Comment(0)
Z
5

another way as implementing inner ActionListener (with setActionCommand(String actionCommand)) for whole JMenu is wrote java.swing.Action for each of JMenuItem or implements EventHandler (seems like as valid for all Listeners that I tried)

example about JButtons and with implemented ActionListener and EventHandler (both Listeners firing events)

EDIT: EventHandler os too hacky, because in Swing aren't another direct method how to call code_block by String value

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.EventHandler;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** based on  @see http://stackoverflow.com/questions/7702697 */
public class GridButtonPanel extends JPanel {

    private static final int N = 2;
    private final List<GridButton> list = new ArrayList<GridButton>();

    public GridButtonPanel() {
        super(new GridLayout(N, N));
        for (int i = 0; i < N * N; i++) {
            int row = i / N;
            int col = i % N;
            GridButton gb = new GridButton(row, col);
            gb.addActionListener((ActionListener) EventHandler.create(ActionListener.class, this,
                    "actionName" + row + "A" + col));
            list.add(gb);
            this.add(gb);
        }
    }

    public void actionName0A0() {
        System.out.println(" Grid at Row 0, Column 0 ");
    }

    public void actionName0A1() {
        System.out.println(" Grid at Row 0, Column 1 ");
    }

    public void actionName1A0() {
        System.out.println(" Grid at Row 1, Column 0 ");
    }

    public void actionName1A1() {
        System.out.println(" Grid at Row 1, Column 1 ");
    }

    private GridButton getGridButton(int r, int c) {
        int index = r * N + c;
        return list.get(index);
    }

    private class GridButton extends JButton {

        private int row;
        private int col;

        public GridButton(int row, int col) {
            super("Row - " + row + ",  Col - " + col);
            this.row = row;
            this.col = col;
            this.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    int r = GridButton.this.row;
                    int c = GridButton.this.col;
                    GridButton gb = GridButtonPanel.this.getGridButton(r, c);
                    System.out.println("r" + r + ",c" + c
                            + " " + (GridButton.this == gb)
                            + " " + (GridButton.this.equals(gb)));
                }
            });
        }
    }

    private void display() {
        JFrame f = new JFrame("GridButton");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new GridButtonPanel().display();
            }
        });
    }
}
Zenia answered 25/1, 2012 at 17:39 Comment(0)
A
4

Why aren't you calling setActionCommand on the menuitem. Instead of using setName, if you call setActionCommand, you should get what you expect when you call getActionCommand

Also, its label, not lable.

Amaryllis answered 25/1, 2012 at 17:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.