30

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

0

5 Answers 5

46

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();
    }
});
3
  • while this is true, you skip all EventListeners associated with the close event when you do it that way...
    – Vogel612
    Commented Aug 27, 2014 at 14:19
  • 3
    How to dispose this(current) JFrame??? Commented Mar 16, 2017 at 21:14
  • What if I just want to close the frame but not kill the event? Commented Sep 26, 2018 at 11:32
18
JButton b3 = new JButton("CLOSE");

b3.setBounds(50, 375, 250, 50);

b3.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    {
        System.exit(0);
    }
});
4
  • 7
    Great for single window applications, but if you use this to close a single window, it will close all others as well... Commented Oct 20, 2016 at 6:31
  • @acp i want to close only same class not previous class in java swing Commented Jul 20, 2017 at 9:16
  • System.exit(0); method is working.. and also dispose(); method also good. Commented May 5, 2019 at 13:24
  • 1
    I don't think using System::exit is a good idea, since invoking System.exit() shuts down the whole JVM.
    – timlg07
    Commented Jul 2, 2020 at 18:29
9

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).

4

You can use super.dispose() method which is more similar to close operation.

1
  • 2
    thanks for trying to be helpful :-) Unfortunately, your answer doesn't add much useful, to the extent to being wrong (the listener's super has no dispose, no need to call the frame's super, see @lins314159 for complete details)
    – kleopatra
    Commented Mar 26, 2012 at 10:13
2

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

1
  • setVisible() method is not working for me. i also thought i can do close the application by doing set the visibility = false... i used System.out(0); method. it works.. dispose() method is also working. Commented May 5, 2019 at 13:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.