Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Message when closing JFrame Window

I have a Java Program containing a class Application inheriting from JFrame.

I want to display a message which asks the user if he wants to exit the program upon clicking the X button at the top right of the window.

This is my code so far:

I got this code from a tutorial I found online. I coded the WindowClosing event handler myself. However, I have trouble registering the window listener (addWindowListener). It is telling me that WindowAdapter is abstract and cannot be instantiated.

How can I solve this problem please?

like image 497
Matthew Avatar asked Nov 16 '12 15:11

Matthew


People also ask

How to programmatically close a JFrame?

Not only to close the JFrame but also to trigger WindowListener events, try this: Best way to close a Swing frame programmatically is to make it behave like it would when the "X" button is pressed. To do that you will need to implement WindowAdapter that suits your needs and set frame's default close operation to do nothing (DO_NOTHING_ON_CLOSE).

How to close a JFrame with action listener in Java?

At first create a button and frame − JFrame frame = new JFrame (); JButton button = new JButton ("Click to Close!"); Now, close the JFrame on the click of the above button with Action Listener − When you will click on the button “Click to Close”, the frame will close.

How to handle window closing events in Java?

Basically all you have to do to handle window closing events is: Use addWindowListener to add a window listener to the JFrame Override windowClosing method of WindowAdapter to handle a window closing event //creating and showing this application's GUI. This is an example on how to handle Window Closing Events in Java.

How to listen to the window closing event of a frame?

Second, add a WindowListener for the frame. The listener will listen to the windowClosing () event which is triggered immediately when the user clicks the close button, before the window is disposed. For example:


2 Answers

Basically, you got it almost correct. There are a few things not put together correctly and a typo.

First remove your WindowClosing method (it's window, not Window) Then replace your addWindowListener(new WindowAdapter()); with the code below

addWindowListener(new WindowAdapter() {
  public void windowClosing(WindowEvent e) {
    int confirmed = JOptionPane.showConfirmDialog(null, 
        "Are you sure you want to exit the program?", "Exit Program Message Box",
        JOptionPane.YES_NO_OPTION);

    if (confirmed == JOptionPane.YES_OPTION) {
      dispose();
    }
  }
});
like image 97
Dan D. Avatar answered Oct 26 '22 08:10

Dan D.


i got this in two minutes coding....

First is set the j frame default closing event in Exit_on_close. Second create a class called "Window Closing Event Handler" and then call it in the i nit stage.

private void WindowClosingEventHandler(){ addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { int confirmed = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit this application?", "Exit Program Message Box",JOptionPane.YES_NO_OPTION);

    if (confirmed == JOptionPane.YES_OPTION) {
        try{
            String login=txtuserid.getText();
            Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/repair", "root", "");
            Statement st = conn.createStatement();
            String update = "UPDATE user set User_Status=0 where UserID='"+ login +"'";
            st.executeUpdate(update);  
            dispose();
            Login2 dialog = new Login2(new javax.swing.JFrame(), true);
            dialog.setVisible(true);
        }catch(SQLException | HeadlessException q){
            JOptionPane.showMessageDialog(null, q);
        }
        System.exit(0);
    }
    else{
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
    }
}
});
}
like image 25
Synester Avatar answered Oct 26 '22 10:10

Synester