Giving JMenuItem’s name to it’s ActionListener

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();
            }
        });
    }
}

Leave a Comment