Note that there are some explanatory texts on larger screens.

plurals
  1. POJMenuItem Constructor Not Accepting Action
    primarykey
    data
    text
    <p><code>JMenuItem</code> has the following constructor: (Source: <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/javax/swing/JMenuItem.java#JMenuItem" rel="nofollow">GrepCode</a>)</p> <pre><code>public JMenuItem(Action a) { this(); setAction(a); } </code></pre> <p>However, when my code has</p> <pre><code>import javax.swing.*; import java.awt.event.ActionEvent; public class ActionTest extends JApplet { private final JFrame frame = new JFrame("Title"); private final JMenuBar menuBar = new JMenuBar(); private final JMenu fileMenu = new JMenu("File"); protected Action someAction; private JMenuItem someButton = new JMenuItem(someAction); public ActionTest() {} @Override public final void init() { frame.setJMenuBar(menuBar); menuBar.add(fileMenu); fileMenu.add(someButton); someButton.setText("Button"); someAction = new AbstractAction("Title") { public void actionPerformed(ActionEvent event) { //do stuff } }; frame.setVisible(true); } public static void main(String[] args) { JApplet applet = new ActionTest(); applet.init(); } } </code></pre> <p>and I press the <code>JMenuItem</code>, actionPerformed() is not even called.</p> <p>Is this a bug, or is my approach completely wrong?</p> <p>After doing more research, I find that <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/javax/swing/event/EventListenerList.java#EventListenerList.add%28java.lang.Class%2Cjava.util.EventListener%29" rel="nofollow">this</a> is the method that it eventually boils down to. It seems to implement a shallow copy, which <a href="http://en.wikipedia.org/w/index.php?title=Object_copy&amp;oldid=493063601#Shallow_copy" rel="nofollow">should simply point to the same memory block that I gave it in the constructor</a>. </p> <p>The same thing should be occurring when I add the file menu to the menu bar. When the file menu is added, it references the memory block. Whatever is inside that memory block is what is displayed. Then, I add the menu item and it appears in the <code>JMenu</code>. </p> <p>Somehow it is different when I'm dealing with <code>Action</code>s or constructors. Could somebody explain the difference?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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