Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The quick-fix for your problem is to provide a handler so that you can attach the ActionListener. You make the JMenus in <code>main()</code>, but you don't have visibility to them in your <code>DrawingPanel</code>. A cheap way to get this done would be to pass the JMenuItems into your DrawingPanel constructor. Modify the constructor to something like:</p> <pre><code>DrawingPanel(JMenuItem save, JMenuItem open) { // the stuff you already got this.save = save; this.open = open; save.addActionListener(this); open.addActionListener(this); } </code></pre> <p>Then you'd pass them into your DrawingPanel in <code>main</code>:</p> <pre><code>JMenuItem loadMenuItem = new JMenuItem("Load"); JMenuItem saveMenuItem = new JMenuItem("Save"); ... frame.getContentPane(new DrawingPanel(saveMenuItem, loadMenuItem)); </code></pre> <p>From a Java-style perspective, it's not clear whether <code>DrawingPanel</code> is the best place to handle save and load actions. As others have mentioned, creating (separate) Actions for each JMenuItem is often a better pattern. In your case, you might be able to provide other accessor or helper methods inside DrawingPanel that would give the saver/loader workers the info they'd need to do their Action.</p> <hr> <p><strong>Edit:</strong> (to address the OP's "new code" Edit)</p> <p>I think the problem with the "new code" is that you're making a <code>new</code> JFileChooser in the <code>actionPerformed</code> method, and not using the existing one that you made earlier and added to the frame. If you make the first one <code>final</code> then you can use the same JFileChooser in the <code>actionPerformed</code> method. </p>
    singulars
    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.
    1. VO
      singulars
      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