Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>another way as implementing inner <a href="http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html">ActionListener</a> (with <code>setActionCommand(String actionCommand)</code>) for whole <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html">JMenu</a> is wrote <a href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">java.swing.Action</a> for each of <code>JMenuItem</code> or implements <a href="http://docs.oracle.com/javase/6/docs/api/java/beans/EventHandler.html">EventHandler</a> (seems like as valid for all <code>Listeners</code> that I tried) </p> <p>example about JButtons and with implemented <code>ActionListener</code> and <code>EventHandler</code> (both <a href="http://docs.oracle.com/javase/tutorial/uiswing/events/index.html">Listeners</a> firing events)</p> <p>EDIT: <code>EventHandler</code> os too hacky, because in Swing aren't another direct method how to call code_block by String value </p> <pre><code>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&lt;GridButton&gt; list = new ArrayList&lt;GridButton&gt;(); public GridButtonPanel() { super(new GridLayout(N, N)); for (int i = 0; i &lt; 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(); } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload