5

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?

Matthew
  • 4,347
  • 18
  • 65
  • 92

3 Answers3

17

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();
    }
  }
});
Duncan Jones
  • 63,838
  • 26
  • 184
  • 242
Dan D.
  • 32,096
  • 5
  • 61
  • 79
  • Thank you so much for your help :) Your solution worked perfectly :) – Matthew Nov 16 '12 at 15:58
  • 1
    @Matthew If you are using Java 5(+), you can use the `@Override` annotation to show that you were intending to extend a method in the superclass, i.e. `windowClosing`. You would have then received a compile error for your typo. – Duncan Jones Nov 16 '12 at 16:00
  • @DuncanJones Thank you for your suggestion :) – Matthew Nov 16 '12 at 16:04
2

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);
    }
}
});
}
Ankur
  • 5,048
  • 19
  • 36
  • 62
Synester
  • 21
  • 1
0

Ok trying again.

You cannot create a new WindowAdapter because WindowAdapter is abstract. Abstract classes cannot be instantiated. You would need to create a subclass of WindowAdapter and implement its abstract methods as public.

http://docs.oracle.com/javase/7/docs/api/java/awt/event/WindowAdapter.html

Scuba Steve
  • 1,493
  • 1
  • 15
  • 43