< prev index next >

src/java.desktop/share/classes/java/awt/event/WindowEvent.java

Print this page




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt.event;
  27 
  28 import java.awt.Window;
  29 import java.lang.annotation.Native;
  30 import sun.awt.AppContext;
  31 import sun.awt.SunToolkit;
  32 
  33 /**
  34  * A low-level event that indicates that a window has changed its status. This
  35  * low-level event is generated by a Window object when it is opened, closed,
  36  * activated, deactivated, iconified, or deiconified, or when focus is
  37  * transferred into or out of the Window.
  38  * <P>
  39  * The event is passed to every <code>WindowListener</code>
  40  * or <code>WindowAdapter</code> object which registered to receive such
  41  * events using the window's <code>addWindowListener</code> method.
  42  * (<code>WindowAdapter</code> objects implement the
  43  * <code>WindowListener</code> interface.) Each such listener object
  44  * gets this <code>WindowEvent</code> when the event occurs.
  45  * <p>
  46  * An unspecified behavior will be caused if the {@code id} parameter
  47  * of any particular {@code WindowEvent} instance is not
  48  * in the range from {@code WINDOW_FIRST} to {@code WINDOW_LAST}.
  49  *
  50  * @author Carl Quinn
  51  * @author Amy Fowler
  52  *
  53  * @see WindowAdapter
  54  * @see WindowListener
  55  * @see <a href="http://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html">Tutorial: Writing a Window Listener</a>
  56  *
  57  * @since 1.1
  58  */
  59 public class WindowEvent extends ComponentEvent {
  60 
  61     /**
  62      * The first number in the range of ids used for window events.
  63      */
  64     public static final int WINDOW_FIRST        = 200;


 159      *
 160      * @see #getOppositeWindow
 161      * @since 1.4
 162      */
 163     transient Window opposite;
 164 
 165     /**
 166      * TBS
 167      */
 168     int oldState;
 169     int newState;
 170 
 171 
 172     /*
 173      * JDK 1.1 serialVersionUID
 174      */
 175     private static final long serialVersionUID = -1567959133147912127L;
 176 
 177 
 178     /**
 179      * Constructs a <code>WindowEvent</code> object.
 180      * <p>This method throws an
 181      * <code>IllegalArgumentException</code> if <code>source</code>
 182      * is <code>null</code>.
 183      *
 184      * @param source    The <code>Window</code> object
 185      *                    that originated the event
 186      * @param id        An integer indicating the type of event.
 187      *                     For information on allowable values, see
 188      *                     the class description for {@link WindowEvent}
 189      * @param opposite  The other window involved in the focus or activation
 190      *                      change, or <code>null</code>
 191      * @param oldState  Previous state of the window for window state change event.
 192      *                  See {@code #getOldState()} for allowable values
 193      * @param newState  New state of the window for window state change event.
 194      *                  See {@code #getNewState()} for allowable values
 195      * @throws IllegalArgumentException if <code>source</code> is null
 196      * @see #getWindow()
 197      * @see #getID()
 198      * @see #getOppositeWindow()
 199      * @see #getOldState()
 200      * @see #getNewState()
 201      * @since 1.4
 202      */
 203     public WindowEvent(Window source, int id, Window opposite,
 204                        int oldState, int newState)
 205     {
 206         super(source, id);
 207         this.opposite = opposite;
 208         this.oldState = oldState;
 209         this.newState = newState;
 210     }
 211 
 212     /**
 213      * Constructs a <code>WindowEvent</code> object with the
 214      * specified opposite <code>Window</code>. The opposite
 215      * <code>Window</code> is the other <code>Window</code>
 216      * involved in this focus or activation change.
 217      * For a <code>WINDOW_ACTIVATED</code> or
 218      * <code>WINDOW_GAINED_FOCUS</code> event, this is the
 219      * <code>Window</code> that lost activation or focus.
 220      * For a <code>WINDOW_DEACTIVATED</code> or
 221      * <code>WINDOW_LOST_FOCUS</code> event, this is the
 222      * <code>Window</code> that gained activation or focus.
 223      * If this focus change occurs with a native application, with a
 224      * Java application in a different VM, or with no other
 225      * <code>Window</code>, then the opposite Window is <code>null</code>.
 226      * <p>This method throws an
 227      * <code>IllegalArgumentException</code> if <code>source</code>
 228      * is <code>null</code>.
 229      *
 230      * @param source     The <code>Window</code> object that
 231      *                   originated the event
 232      * @param id        An integer indicating the type of event.
 233      *                     For information on allowable values, see
 234      *                     the class description for {@link WindowEvent}.
 235      *                  It is expected that this constructor will not
 236      *                  be used for other then
 237      *                  {@code WINDOW_ACTIVATED},{@code WINDOW_DEACTIVATED},
 238      *                  {@code WINDOW_GAINED_FOCUS}, or {@code WINDOW_LOST_FOCUS}.
 239      *                  {@code WindowEvent} types,
 240      *                  because the opposite <code>Window</code> of other event types
 241      *                  will always be {@code null}.
 242      * @param opposite   The other <code>Window</code> involved in the
 243      *                   focus or activation change, or <code>null</code>
 244      * @throws IllegalArgumentException if <code>source</code> is null
 245      * @see #getWindow()
 246      * @see #getID()
 247      * @see #getOppositeWindow()
 248      * @since 1.4
 249      */
 250     public WindowEvent(Window source, int id, Window opposite) {
 251         this(source, id, opposite, 0, 0);
 252     }
 253 
 254     /**
 255      * Constructs a <code>WindowEvent</code> object with the specified
 256      * previous and new window states.
 257      * <p>This method throws an
 258      * <code>IllegalArgumentException</code> if <code>source</code>
 259      * is <code>null</code>.
 260      *
 261      * @param source    The <code>Window</code> object
 262      *                  that originated the event
 263      * @param id        An integer indicating the type of event.
 264      *                     For information on allowable values, see
 265      *                     the class description for {@link WindowEvent}.
 266      *                  It is expected that this constructor will not
 267      *                  be used for other then
 268      *                  {@code WINDOW_STATE_CHANGED}
 269      *                  {@code WindowEvent}
 270      *                  types, because the previous and new window
 271      *                  states are meaningless for other event types.
 272      * @param oldState  An integer representing the previous window state.
 273      *                  See {@code #getOldState()} for allowable values
 274      * @param newState  An integer representing the new window state.
 275      *                  See {@code #getNewState()} for allowable values
 276      * @throws IllegalArgumentException if <code>source</code> is null
 277      * @see #getWindow()
 278      * @see #getID()
 279      * @see #getOldState()
 280      * @see #getNewState()
 281      * @since 1.4
 282      */
 283     public WindowEvent(Window source, int id, int oldState, int newState) {
 284         this(source, id, null, oldState, newState);
 285     }
 286 
 287     /**
 288      * Constructs a <code>WindowEvent</code> object.
 289      * <p>This method throws an
 290      * <code>IllegalArgumentException</code> if <code>source</code>
 291      * is <code>null</code>.
 292      *
 293      * @param source The <code>Window</code> object that originated the event
 294      * @param id     An integer indicating the type of event.
 295      *                     For information on allowable values, see
 296      *                     the class description for {@link WindowEvent}.
 297      * @throws IllegalArgumentException if <code>source</code> is null
 298      * @see #getWindow()
 299      * @see #getID()
 300      */
 301     public WindowEvent(Window source, int id) {
 302         this(source, id, null, 0, 0);
 303     }
 304 
 305     /**
 306      * Returns the originator of the event.
 307      *
 308      * @return the Window object that originated the event
 309      */
 310     public Window getWindow() {
 311         return (source instanceof Window) ? (Window)source : null;
 312     }
 313 
 314     /**
 315      * Returns the other Window involved in this focus or activation change.
 316      * For a WINDOW_ACTIVATED or WINDOW_GAINED_FOCUS event, this is the Window
 317      * that lost activation or focus. For a WINDOW_DEACTIVATED or


 319      * focus. For any other type of WindowEvent, or if the focus or activation
 320      * change occurs with a native application, with a Java application in a
 321      * different VM or context, or with no other Window, null is returned.
 322      *
 323      * @return the other Window involved in the focus or activation change, or
 324      *         null
 325      * @since 1.4
 326      */
 327     public Window getOppositeWindow() {
 328         if (opposite == null) {
 329             return null;
 330         }
 331 
 332         return (SunToolkit.targetToAppContext(opposite) ==
 333                 AppContext.getAppContext())
 334             ? opposite
 335             : null;
 336     }
 337 
 338     /**
 339      * For <code>WINDOW_STATE_CHANGED</code> events returns the
 340      * previous state of the window. The state is
 341      * represented as a bitwise mask.
 342      * <ul>
 343      * <li><code>NORMAL</code>
 344      * <br>Indicates that no state bits are set.
 345      * <li><code>ICONIFIED</code>
 346      * <li><code>MAXIMIZED_HORIZ</code>
 347      * <li><code>MAXIMIZED_VERT</code>
 348      * <li><code>MAXIMIZED_BOTH</code>
 349      * <br>Concatenates <code>MAXIMIZED_HORIZ</code>
 350      * and <code>MAXIMIZED_VERT</code>.
 351      * </ul>
 352      *
 353      * @return a bitwise mask of the previous window state
 354      * @see java.awt.Frame#getExtendedState()
 355      * @since 1.4
 356      */
 357     public int getOldState() {
 358         return oldState;
 359     }
 360 
 361     /**
 362      * For <code>WINDOW_STATE_CHANGED</code> events returns the
 363      * new state of the window. The state is
 364      * represented as a bitwise mask.
 365      * <ul>
 366      * <li><code>NORMAL</code>
 367      * <br>Indicates that no state bits are set.
 368      * <li><code>ICONIFIED</code>
 369      * <li><code>MAXIMIZED_HORIZ</code>
 370      * <li><code>MAXIMIZED_VERT</code>
 371      * <li><code>MAXIMIZED_BOTH</code>
 372      * <br>Concatenates <code>MAXIMIZED_HORIZ</code>
 373      * and <code>MAXIMIZED_VERT</code>.
 374      * </ul>
 375      *
 376      * @return a bitwise mask of the new window state
 377      * @see java.awt.Frame#getExtendedState()
 378      * @since 1.4
 379      */
 380     public int getNewState() {
 381         return newState;
 382     }
 383 
 384     /**
 385      * Returns a parameter string identifying this event.
 386      * This method is useful for event-logging and for debugging.
 387      *
 388      * @return a string identifying the event and its attributes
 389      */
 390     public String paramString() {
 391         String typeStr;
 392         switch(id) {
 393           case WINDOW_OPENED:




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt.event;
  27 
  28 import java.awt.Window;
  29 import java.lang.annotation.Native;
  30 import sun.awt.AppContext;
  31 import sun.awt.SunToolkit;
  32 
  33 /**
  34  * A low-level event that indicates that a window has changed its status. This
  35  * low-level event is generated by a Window object when it is opened, closed,
  36  * activated, deactivated, iconified, or deiconified, or when focus is
  37  * transferred into or out of the Window.
  38  * <P>
  39  * The event is passed to every {@code WindowListener}
  40  * or {@code WindowAdapter} object which registered to receive such
  41  * events using the window's {@code addWindowListener} method.
  42  * ({@code WindowAdapter} objects implement the
  43  * {@code WindowListener} interface.) Each such listener object
  44  * gets this {@code WindowEvent} when the event occurs.
  45  * <p>
  46  * An unspecified behavior will be caused if the {@code id} parameter
  47  * of any particular {@code WindowEvent} instance is not
  48  * in the range from {@code WINDOW_FIRST} to {@code WINDOW_LAST}.
  49  *
  50  * @author Carl Quinn
  51  * @author Amy Fowler
  52  *
  53  * @see WindowAdapter
  54  * @see WindowListener
  55  * @see <a href="http://docs.oracle.com/javase/tutorial/uiswing/events/windowlistener.html">Tutorial: Writing a Window Listener</a>
  56  *
  57  * @since 1.1
  58  */
  59 public class WindowEvent extends ComponentEvent {
  60 
  61     /**
  62      * The first number in the range of ids used for window events.
  63      */
  64     public static final int WINDOW_FIRST        = 200;


 159      *
 160      * @see #getOppositeWindow
 161      * @since 1.4
 162      */
 163     transient Window opposite;
 164 
 165     /**
 166      * TBS
 167      */
 168     int oldState;
 169     int newState;
 170 
 171 
 172     /*
 173      * JDK 1.1 serialVersionUID
 174      */
 175     private static final long serialVersionUID = -1567959133147912127L;
 176 
 177 
 178     /**
 179      * Constructs a {@code WindowEvent} object.
 180      * <p>This method throws an
 181      * {@code IllegalArgumentException} if {@code source}
 182      * is {@code null}.
 183      *
 184      * @param source    The {@code Window} object
 185      *                    that originated the event
 186      * @param id        An integer indicating the type of event.
 187      *                     For information on allowable values, see
 188      *                     the class description for {@link WindowEvent}
 189      * @param opposite  The other window involved in the focus or activation
 190      *                      change, or {@code null}
 191      * @param oldState  Previous state of the window for window state change event.
 192      *                  See {@code #getOldState()} for allowable values
 193      * @param newState  New state of the window for window state change event.
 194      *                  See {@code #getNewState()} for allowable values
 195      * @throws IllegalArgumentException if {@code source} is null
 196      * @see #getWindow()
 197      * @see #getID()
 198      * @see #getOppositeWindow()
 199      * @see #getOldState()
 200      * @see #getNewState()
 201      * @since 1.4
 202      */
 203     public WindowEvent(Window source, int id, Window opposite,
 204                        int oldState, int newState)
 205     {
 206         super(source, id);
 207         this.opposite = opposite;
 208         this.oldState = oldState;
 209         this.newState = newState;
 210     }
 211 
 212     /**
 213      * Constructs a {@code WindowEvent} object with the
 214      * specified opposite {@code Window}. The opposite
 215      * {@code Window} is the other {@code Window}
 216      * involved in this focus or activation change.
 217      * For a {@code WINDOW_ACTIVATED} or
 218      * {@code WINDOW_GAINED_FOCUS} event, this is the
 219      * {@code Window} that lost activation or focus.
 220      * For a {@code WINDOW_DEACTIVATED} or
 221      * {@code WINDOW_LOST_FOCUS} event, this is the
 222      * {@code Window} that gained activation or focus.
 223      * If this focus change occurs with a native application, with a
 224      * Java application in a different VM, or with no other
 225      * {@code Window}, then the opposite Window is {@code null}.
 226      * <p>This method throws an
 227      * {@code IllegalArgumentException} if {@code source}
 228      * is {@code null}.
 229      *
 230      * @param source     The {@code Window} object that
 231      *                   originated the event
 232      * @param id        An integer indicating the type of event.
 233      *                     For information on allowable values, see
 234      *                     the class description for {@link WindowEvent}.
 235      *                  It is expected that this constructor will not
 236      *                  be used for other then
 237      *                  {@code WINDOW_ACTIVATED},{@code WINDOW_DEACTIVATED},
 238      *                  {@code WINDOW_GAINED_FOCUS}, or {@code WINDOW_LOST_FOCUS}.
 239      *                  {@code WindowEvent} types,
 240      *                  because the opposite {@code Window} of other event types
 241      *                  will always be {@code null}.
 242      * @param opposite   The other {@code Window} involved in the
 243      *                   focus or activation change, or {@code null}
 244      * @throws IllegalArgumentException if {@code source} is null
 245      * @see #getWindow()
 246      * @see #getID()
 247      * @see #getOppositeWindow()
 248      * @since 1.4
 249      */
 250     public WindowEvent(Window source, int id, Window opposite) {
 251         this(source, id, opposite, 0, 0);
 252     }
 253 
 254     /**
 255      * Constructs a {@code WindowEvent} object with the specified
 256      * previous and new window states.
 257      * <p>This method throws an
 258      * {@code IllegalArgumentException} if {@code source}
 259      * is {@code null}.
 260      *
 261      * @param source    The {@code Window} object
 262      *                  that originated the event
 263      * @param id        An integer indicating the type of event.
 264      *                     For information on allowable values, see
 265      *                     the class description for {@link WindowEvent}.
 266      *                  It is expected that this constructor will not
 267      *                  be used for other then
 268      *                  {@code WINDOW_STATE_CHANGED}
 269      *                  {@code WindowEvent}
 270      *                  types, because the previous and new window
 271      *                  states are meaningless for other event types.
 272      * @param oldState  An integer representing the previous window state.
 273      *                  See {@code #getOldState()} for allowable values
 274      * @param newState  An integer representing the new window state.
 275      *                  See {@code #getNewState()} for allowable values
 276      * @throws IllegalArgumentException if {@code source} is null
 277      * @see #getWindow()
 278      * @see #getID()
 279      * @see #getOldState()
 280      * @see #getNewState()
 281      * @since 1.4
 282      */
 283     public WindowEvent(Window source, int id, int oldState, int newState) {
 284         this(source, id, null, oldState, newState);
 285     }
 286 
 287     /**
 288      * Constructs a {@code WindowEvent} object.
 289      * <p>This method throws an
 290      * {@code IllegalArgumentException} if {@code source}
 291      * is {@code null}.
 292      *
 293      * @param source The {@code Window} object that originated the event
 294      * @param id     An integer indicating the type of event.
 295      *                     For information on allowable values, see
 296      *                     the class description for {@link WindowEvent}.
 297      * @throws IllegalArgumentException if {@code source} is null
 298      * @see #getWindow()
 299      * @see #getID()
 300      */
 301     public WindowEvent(Window source, int id) {
 302         this(source, id, null, 0, 0);
 303     }
 304 
 305     /**
 306      * Returns the originator of the event.
 307      *
 308      * @return the Window object that originated the event
 309      */
 310     public Window getWindow() {
 311         return (source instanceof Window) ? (Window)source : null;
 312     }
 313 
 314     /**
 315      * Returns the other Window involved in this focus or activation change.
 316      * For a WINDOW_ACTIVATED or WINDOW_GAINED_FOCUS event, this is the Window
 317      * that lost activation or focus. For a WINDOW_DEACTIVATED or


 319      * focus. For any other type of WindowEvent, or if the focus or activation
 320      * change occurs with a native application, with a Java application in a
 321      * different VM or context, or with no other Window, null is returned.
 322      *
 323      * @return the other Window involved in the focus or activation change, or
 324      *         null
 325      * @since 1.4
 326      */
 327     public Window getOppositeWindow() {
 328         if (opposite == null) {
 329             return null;
 330         }
 331 
 332         return (SunToolkit.targetToAppContext(opposite) ==
 333                 AppContext.getAppContext())
 334             ? opposite
 335             : null;
 336     }
 337 
 338     /**
 339      * For {@code WINDOW_STATE_CHANGED} events returns the
 340      * previous state of the window. The state is
 341      * represented as a bitwise mask.
 342      * <ul>
 343      * <li>{@code NORMAL}
 344      * <br>Indicates that no state bits are set.
 345      * <li>{@code ICONIFIED}
 346      * <li>{@code MAXIMIZED_HORIZ}
 347      * <li>{@code MAXIMIZED_VERT}
 348      * <li>{@code MAXIMIZED_BOTH}
 349      * <br>Concatenates {@code MAXIMIZED_HORIZ}
 350      * and {@code MAXIMIZED_VERT}.
 351      * </ul>
 352      *
 353      * @return a bitwise mask of the previous window state
 354      * @see java.awt.Frame#getExtendedState()
 355      * @since 1.4
 356      */
 357     public int getOldState() {
 358         return oldState;
 359     }
 360 
 361     /**
 362      * For {@code WINDOW_STATE_CHANGED} events returns the
 363      * new state of the window. The state is
 364      * represented as a bitwise mask.
 365      * <ul>
 366      * <li>{@code NORMAL}
 367      * <br>Indicates that no state bits are set.
 368      * <li>{@code ICONIFIED}
 369      * <li>{@code MAXIMIZED_HORIZ}
 370      * <li>{@code MAXIMIZED_VERT}
 371      * <li>{@code MAXIMIZED_BOTH}
 372      * <br>Concatenates {@code MAXIMIZED_HORIZ}
 373      * and {@code MAXIMIZED_VERT}.
 374      * </ul>
 375      *
 376      * @return a bitwise mask of the new window state
 377      * @see java.awt.Frame#getExtendedState()
 378      * @since 1.4
 379      */
 380     public int getNewState() {
 381         return newState;
 382     }
 383 
 384     /**
 385      * Returns a parameter string identifying this event.
 386      * This method is useful for event-logging and for debugging.
 387      *
 388      * @return a string identifying the event and its attributes
 389      */
 390     public String paramString() {
 391         String typeStr;
 392         switch(id) {
 393           case WINDOW_OPENED:


< prev index next >