-2

I'm looking for clarification about an answer given to an old question: Do something when the close button is clicked on a JFrame

In the answer, Ravindra Gullapalli suggested this code:

import javax.swing.JOptionPane;
/*Some piece of code*/
frame.addWindowListener(new java.awt.event.WindowAdapter() {
    @Override
    public void windowClosing(java.awt.event.WindowEvent windowEvent) {
        if (JOptionPane.showConfirmDialog(frame, 
            "Are you sure to close this window?", "Really Closing?", 
            JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){
            System.exit(0);
        }
    }
});

Firstly, is this the proper way to do this in Netbeans?

Secondly, what is "frame"? (the first word in the 3rd line of code in the answer as well as the first parameter of showConfirmDialog). According to swing API, this should be of type Component. However, when I replaced this with the Title property of my JFrame, I still got a "cannot find symbol" error. Is the Component name different than the JFrame title, and if so where can I find it? In the NetBeans Navigator, it just says [JFrame] and does not display the Component name, unlike all the other Form objects which display and editable Component name next to the type.

Community
  • 1
  • 1
Matt Groth
  • 470
  • 4
  • 20

2 Answers2

2

"frame" is simply assumed to be the variable name for the JFrame in code not shown. If your program has a JFrame or other top-level Window named "frame" and it is in scope at that line of code, you're in business. Otherwise you will have to use the variable name that you currently are using. This is yet another reason to avoid "borrowing" code. Instead learn the concepts, borrow the ideas, and use both to write your own code.


With regard to:

Is the Component name different than the JFrame title, and if so where can I find it?

Not sure what you mean here. Again in the code in the answer, frame is the name of a variable, not a "component name" or "title". This involves Java 101 concepts -- variable names and variable scope, and has nothing to do with Swing or NetBeans.


With regard to:

is this the proper way for NetBeans"

it is if this is the behavior that you're trying to achieve.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
2

Secondly, what is "frame"?

A reference to the frame that contains the close button you just clicked on.

However, when I replaced this with the Title property of my JFrame

Not sure why you would use the "Title" property. That is just a String. You can't open/close a String.

Firstly, is this the proper way to do this in Netbeans?

A better structure for the code in the WindowListener might be:

JFrame frame = (JFrame)windowEvent.getSource();

then you don't have to worry about defining a variable in your class.

Also you can use:

frame.dispose();

instead of System.exit(). The JVM will shut down if this is the last open window.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you, but I do not know what you mean by the WindowListener (maybe this is something more relevant outside of Netbeans, I tried looking into it and found this: http://stackoverflow.com/questions/23393107/adding-listeners-in-netbeans) – Matt Groth Dec 26 '16 at 04:26
  • `I do not know what you mean by the WindowListener` - what? You are using the `addWindowListener(...)` method. Why are you using that method if you don't know what a `WindowListener` is? I suggest you read the section from the Swing tutorial on [How to Write a WindowListener](http://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html) for basic information and examples. Keep the link handy for all Swing basics. – camickr Dec 26 '16 at 05:02