4

WindowConstants is defined as this:

public interface WindowConstants
{
    public static final int DO_NOTHING_ON_CLOSE = 0;

    public static final int HIDE_ON_CLOSE = 1;

    public static final int DISPOSE_ON_CLOSE = 2;

    public static final int EXIT_ON_CLOSE = 3;

}

JFrame is defined as this:

public class JFrame  extends Frame implements WindowConstants,
                                              Accessible,
                                              RootPaneContainer,
                              TransferHandler.HasGetTransferHandler
{
    /**
     * The exit application default window close operation. If a window
     * has this set as the close operation and is closed in an applet,
     * a <code>SecurityException</code> may be thrown.
     * It is recommended you only use this in an application.
     * <p>
     * @since 1.3
     */
    public static final int EXIT_ON_CLOSE = 3;

Why the EXIT_ON_CLOSE is redefined? And since it is final in the WindowConstants interface, how could it be redefined?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • Why do you need the EXIT_ON_CLOSE to be redefined? – StanislavL Dec 08 '14 at 12:43
  • No, I didn't redefine it. And I don't think need to. But it is defined in the JFrame as the source code shows. I just want to know why. – smwikipedia Dec 08 '14 at 12:45
  • because in API is there HIDE_ON_CLOSE as default, it hasn't nothing with EXIT_ON_CLOSE :-) – mKorbel Dec 08 '14 at 12:46
  • changed in/from Java5/6 (latest 5 and new 6 versions) – mKorbel Dec 08 '14 at 12:47
  • @mKorbel I see there's a `private int defaultCloseOperation = HIDE_ON_CLOSE;` in `JFrame`. But I still don't get why there is a re-definition of `EXIT_ON_CLOSE` in `JFrame`. – smwikipedia Dec 08 '14 at 12:52
  • to try output from JFrame.get/setDefaultCloseOperation....., there isn't reason, without casting to AWT.Frame, those nested/inherits whatever from AWT are implemented as methods in swing.JFrame – mKorbel Dec 08 '14 at 12:54

1 Answers1

7

In Java 1.3, when this was all added, EXIT_ON_CLOSE was relevant only to JFrame and not to other implementations of WindowConstants. As such - it was not present in WindowConstants and was defined in JFrame. The 3 other XXX_ON_CLOSE options were in the interface. (English Javadoc is not online anymore, though still downloadable, so no reference here. If you search for "WindowConstants Java 1.3" you'll get a Japanese version of the Javadoc - but since the page structure is the same, you can still see the point)

It was later (1.4) moved to WindowConstants, however the field was not removed from JFrame due to compatibility issues.

There is no redefining there. What's happening is shadowing. I.e. the JFrame field hides (but does not eliminate) the WindowConstants field.

Community
  • 1
  • 1
Ordous
  • 3,844
  • 15
  • 25
  • Thanks! And for others to reference, I paste the Japanese link of Java 1.3 `WindowConstants` here: https://docs.oracle.com/javase/jp/1.3/api/javax/swing/WindowConstants.html – smwikipedia Dec 09 '14 at 11:51