1

Particularly new to Java, and I have a simple style/design/syntax question: when creating a JFrame:

JFrame frame = new JFrame(NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

is the JFrame before EXIT_ON_CLOSE necessary? Isn't it already specified with using frame.setDefault...?

One last question is are there any particular benefits to using JFrame instead of Frame? Pros & Cons?

Thank you in advance!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Otanan
  • 459
  • 3
  • 12

5 Answers5

3

is the JFrame before EXIT_ON_CLOSE necessary? Isn't it already specified with using frame.setDefault...?

It's necessary cause JFrame.EXIT_ON_CLOSE is a constant (class level variable). that when you set, exit the application using the System exit method.

you can ommited it, if you make a static import like this.

Example with static import:

import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class Test{

  public static void main(String args[]) throws CloneNotSupportedException {
       JFrame frame = new JFrame();
       frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }        
}

One last question is are there any particular benefits to using JFrame instead of Frame? Pros & Cons?

JFrame is from swing, and Frame is more outdated from awt. Here you have more information Swing vs AWT

Community
  • 1
  • 1
nachokk
  • 14,363
  • 4
  • 24
  • 53
  • It's necessary cause JFrame.EXIT_ON_CLOSE it's a constant(class level variable ). you can ommited if you make a static import., not wrong JFrame.EXIT_ON_CLOSE terminating current JVM – mKorbel Dec 30 '13 at 19:55
  • @mKorbel don't understand your english , sorry xD – nachokk Dec 30 '13 at 19:57
  • Thanks for the help! but I don't understand the import statement nor the WindowsConstant, do you mean my NAME constant? bad practice? Could you go a little further in depth? Thank You! – Otanan Dec 30 '13 at 20:02
  • @Otanan That comment was a side note to swing designers, not you.. is a bad practice using an interface wich holds only constants and no methods.. JFrame implements WindowConstants.. I delete that comment cause creates confussion – nachokk Dec 30 '13 at 20:05
  • 1
    @nachokk thank you! turns out I was already extending JFrame which is why it didn't give me an error, and that explained the whole concept of the passing in a constant. This really helped me understand, I appreciate it so much! – Otanan Dec 30 '13 at 20:18
  • @Otanan then the example you provided in your question is not correct cause there you are not extending , but im glad to hear that you understand and you solve yourself! – nachokk Dec 30 '13 at 20:20
2

EXIT_ON_CLOSE is a static variable belonging to the JFrame class. It's pretty standard to use it by specifying JFrame.EXIT_ON_CLOSE, but you could also import it if it really bothers you.

The Math class has similar static variables.

JFrame is Swing, Frame is AWT. That's a good place to start a google search.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
2

is the JFrame before EXIT_ON_CLOSE necessary? Isn't it already specified with using frame.setDefault...?

Yes, because you need to provide correct exit behavior you want when closing the Application frame: There are four such constant available:

  • DO_NOTHING_ON_CLOSE: Don't do anything; Use it if you require the program to handle the closing operation and do some thing on window close event; you can use Window Listeners here

  • HIDE_ON_CLOSE : Automatically hide the frame after invoking any registered WindowListener objects. So register WindowListener to your JFrame if required.

  • DISPOSE_ON_CLOSE : Automatically hide and dispose the frame after invoking any registered WindowListener objects.

  • EXIT_ON_CLOSE: Exit the application using the System exit method. Use this only in applications.

All this constants are defined in WindowCOnstants interface. However, as you wanted to know the default value set as the Default close operation: it is HIDE_ON_CLOSE

Reference:

  1. setDefaultCloseOperation(int operation)
Sage
  • 15,290
  • 3
  • 33
  • 38
  • That's 90% truth, JFrame.EXIT_ON_CLOSE hides or shadow (never remember wich one) windowsConstant.EXIT_ON_CLOSE so that constant is defined in JFrame. i don't know why they don't remove it, JFrame.EXIT_ON_CLOSE since java 1.3 and WindowConstant.EXIT_ON_CLOSE since java 1.4 – nachokk Dec 30 '13 at 20:17
  • @nachokk EXIT_ON_CLOSE to shutdown current JVM instance (note there is an issue with one of non_deamon threads, but not main issue in this question) – mKorbel Dec 30 '13 at 20:21
  • @mKorbel i said another thing xD don't know why we never understood each other is not the first time haha – nachokk Dec 30 '13 at 20:24
  • im not clear i think, my question is : why exist in JFrame class a constant that "hides/shadows"(don't remember now which is one) WindowsConstants.EXIT_ON_CLOSE? I know that in JFrame already exist since 1.3 and in windowsConstant since 1.4 but why they don't remove at their time. You can still refer as `JFrame.EXIT_ON_CLOSE` cause implements WindowsConstant – nachokk Dec 30 '13 at 20:28
  • @nachokk, now i get you. Sorry misunderstood. Yes it seems that As `JFrame` is implementation `WindowConstants`, there is still a declaration of `EXIT_ON_CLOSE` which is hiding(Shadowing is not actually correct term here though i am sure you know it) `WindowConstants` fields. And the answer is... :(I don't know and don't tell it anyone) Programming fault may be (they forgot to remove it) :) – Sage Dec 30 '13 at 20:34
  • @Sage the answer always is backward compatibility but i don't see at this point.. i know the difference between hiding and shadowing but never remember wich one is i always have to read :D, you know my english is not too good – nachokk Dec 30 '13 at 20:37
  • @nachokk, uhh don't worry so much about it. I am from Bangladesh and my English is not that good too: This is not something to be mentioned as great, the fact is that: We are participating here sharing our knowledge and that is what really great. :) – Sage Dec 30 '13 at 20:41
1

JFrame.EXIT_ON_CLOSE is a Constant used to control the window-closing operation.
Same as System.exit(0); The Code may like this:

   protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);

        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            switch(defaultCloseOperation) {
              ...
          case EXIT_ON_CLOSE:
                  // This needs to match the checkExit call in
                  // setDefaultCloseOperation
        System.exit(0);
        break;
            }
        }
    }
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
-1

is the JFrame before EXIT_ON_CLOSE necessary? Isn't it already specified with using frame.setDefault...?

Yes it is necessary, because the constant EXIT_ON_CLOSE Is in the JFrame class. The only time when you would not need it is when you use a static import or are extending JFrame.

One last question is are there any particular benefits to using JFrame instead of Frame? Pros & Cons?

JFrame is part of the Swing library, Frame is part of the AWT library. Generally, Swing is more efficient and advanced than Awt

MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
  • You might still need to specify JFrame even when extending JFrame, but a static import is another time in which you would not need to specify the JFrame class. – jzd Dec 30 '13 at 20:00
  • Still have an issue. Mostly with this phrase "The only time". Your first example of extending a the JFrame class is correct, you don't need to specify JFrame, but a static import is another example. – jzd Dec 30 '13 at 20:13