tags:

views:

245

answers:

4

I have the jButton1 private member of JFrame and i wanted to close the frame when the button is clicked.

jButton1.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent e)
            {


            }
        });

I wanted to do super.close() but could not find close for super. Is there some way to refer to the JFrame

+1  A: 

You cat use setVisible () method of JFrame (and set visibility to false) or dispose () method which is more similar to close operation.

Roman
+5  A: 

You will need a reference to the specific frame you want to close but assuming you have the reference dispose() should close the frame.

jButton1.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent e)
                 {
                     frameToClose.dispose();

                 }
        });

Also here is a link to a forum post on sun's forums which is very related: http://forums.sun.com/thread.jspa?threadID=566352

Anton
+1  A: 

It appears to me that you have two issues here. One is that JFrame does not have a close method, which has been addressed in the other answers.

The other is that you're having trouble referencing your JFrame. Within actionPerformed, super refers to ActionListener. To refer to the JFrame instance there, use MyExtendedJFrame.super instead (you should also be able to use MyExtendedJFrame.this, as I see no reason why you'd want to override the behaviour of dispose or setVisible).

lins314159