20

Does anyone know how to make a jbutton close a gui? I think it is like System.CLOSE(0); but that didnt work. it also could be exitActionPerformed(evt);, but that didn't work either. just the line of code will work.

EDIT: never mind guys. the answer was System.exit(0);. thanks for the help though!

peterh
  • 11,875
  • 18
  • 85
  • 108
PulsePanda
  • 1,806
  • 10
  • 33
  • 56

7 Answers7

27

Add your button:

JButton close = new JButton("Close");

Add an ActionListener:

close.addActionListner(new CloseListener());

Add a class for the Listener implementing the ActionListener interface and override its main function:

private class CloseListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        //DO SOMETHING
        System.exit(0);
    }
}

This might be not the best way, but its a point to start. The class for example can be made public and not as a private class inside another one.

Ali Abazari
  • 429
  • 4
  • 20
Baarn
  • 673
  • 11
  • 23
  • `close.addActionListner(new CloseListener());` is misspelled. should be `close.addActionListener(new CloseListener());` – A. Larsson Nov 05 '18 at 10:43
9

By using System.exit(0); you would close the entire process. Is that what you wanted or did you intend to close only the GUI window and allow the process to continue running?

The quickest, easiest and most robust way to simply close a JFrame or JPanel with the click of a JButton is to add an actionListener to the JButton which will execute the line of code below when the JButton is clicked:

this.dispose();

If you are using the NetBeans GUI designer, the easiest way to add this actionListener is to enter the GUI editor window and double click the JButton component. Doing this will automatically create an actionListener and actionEvent, which can be modified manually by you.

Dillon Chaffey
  • 235
  • 2
  • 8
  • Thank you for the dispose() suggestion. But how do you actually kill the GUI thread after the dispose() instruction? – Employee Jul 10 '18 at 10:13
7

See JFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE)1. You might also use EXIT_ON_CLOSE, but it is better to explicitly clean up any running threads, then when the last GUI element becomes invisible, the EDT & JRE will end.

The 'button' to invoke this operation is already on a frame.

  1. See this answer to How to best position Swing GUIs? for a demo. of the DISPOSE_ON_CLOSE functionality.

    The JRE will end after all 3 frames are closed by clicking the X button.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
4

You may use Window#dispose() method to release all of the native screen resources, subcomponents, and all of its owned children.

The System.exit(0) will terminates the currently running Java Virtual Machine.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
3

In Java 8, you can use Lambda expressions to make it simpler.

Close application

JButton btnClose = new JButton("Close");
btnClose.addActionListener(e -> System.exit(0));

Close window

JButton btnClose = new JButton("Close");
btnClose.addActionListener(e -> this.dispose());
ciri-cuervo
  • 1,696
  • 2
  • 18
  • 21
2

Create a method and call it to close the JFrame, for example:

public void CloseJframe(){
    super.dispose();
}
Amazigh
  • 21
  • 3
0
JButton close = new JButton("Close");
close.addActionListener(this);
public void actionPerformed(ActionEvent closing) { 
// getSource() checks for the source of clicked Button , compares with the name of button in which here is close .     
if(closing.getSource()==close)
   System.exit(0);
   // This exit Your GUI 
}
/*Some Answers were asking for @override which is overriding the method the super class or the parent class and creating different objects and etc which makes the answer too long . Note : we just need to import java.awt.*; and java.swing.*; and Adding this command : class className implements actionListener{} */
Niamatullah Bakhshi
  • 1,445
  • 16
  • 27
  • Can you please provide some written information as to how / why this block of code answers the OP's question and why it's different or an improvement on the other posted answers? – wahwahwah Feb 16 '17 at 21:58
  • Sure , Some Answers were asking for @override which is overriding the method the super class or the parent class and creating different objects and etc which makes the answer too long . Note : we just need to import java.awt.*; and java.swing.*; and Adding this command : class className implements actionListener{} . – Niamatullah Bakhshi Feb 16 '17 at 22:13
  • Put that in your answer - not the comment :) – wahwahwah Feb 17 '17 at 06:07