2

I'm trying to write code so that when a user clicks the 'File' tab and selects 'Exit', it exits the whole window I've built.

I am trying to use the dispose(); method, but it gives me the error "The method dispose() is undefined for the type new ActionListener()"

Heres the code

public static void addLayouts(){

        frame = new JFrame();
        frame.setSize(600, 600);
        frame.setTitle("IPFinder");
        GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGap(0, 384, Short.MAX_VALUE)
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGap(0, 362, Short.MAX_VALUE)
        );
        frame.getContentPane().setLayout(groupLayout);

        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);



        JMenu mnEdit = new JMenu("Edit");
        menuBar.add(mnEdit);

        JMenuItem mntmCopyResults = new JMenuItem("Copy Results");
        mnEdit.add(mntmCopyResults);

        JMenu mnAbout = new JMenu("About");
        menuBar.add(mnAbout);

        JMenuItem mntmAboutIpfinder = new JMenuItem("About IPFinder");
        mnAbout.add(mntmAboutIpfinder);
        frame.setVisible(true);



        JMenuItem mntmExit = new JMenuItem("Exit");
        mntmExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        mnFile.add(mntmExit);


    }

I speant about 2 hours on google trying to figure it but i can't get it to work.

I've exhausted all my options at this point so I came here. Any help is apreciated.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
Lion
  • 117
  • 2
  • 5
  • ... I feel incredibly stupid. I completely put it past me that I'm actually closing the entire window, so JFrame, being it, should be closed. I apologize. But I just learned a lot from this. Thank you all, I would give everyone a check but I can only accept one. Thanks again.Sorry for wasting your time. – Lion Jan 25 '13 at 16:57

3 Answers3

5

Use ClassName.this.dispose() if your class extends JFrame or JDialog instead of simply calling dispose() or frame.dispose() if you have a frame variable of such type. In your case it is the second option: frame.dispose();

This way, you tell the compiler that you are calling the dispose() of a Window subtype and not of the anonymous implementation of an ActionListener, which doesn't have a dispose() anyway.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
4

dispose() should be called on JFrame or JDialog, and not on ActionListener.

but it gives me the error "The method dispose() is undefined for the type new ActionListener()"

Exactly. ActionListener has only one method actionPerformed(). http://docs.oracle.com/javase/6/docs/api/java/awt/event/ActionListener.html

Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
2

As I have seen you have defined

frame = new JFrame();

Your class does not extend JFrame. So when you call dispose it throws error since ActionListener class does not have such method.

So use the component which you want to dispose.

Example: frame.dispose();
Amarnath
  • 8,736
  • 10
  • 54
  • 81