AWT Window Class

The class Window is a top level window with no border and no menubar. It uses BorderLayout as default layout manager.
Following is the declaration for java.awt.Window class:
public class Window
   extends Container
      implements Accessible
Window(Frame owner) 
Constructs a new, initially invisible window with the specified Frame as its owner.
Window(Window owner) 
Constructs a new, initially invisible window with the specified Window as its owner.
Window(Window owner, GraphicsConfiguration gc) 
Constructs a new, initially invisible window with the specified owner Window and a GraphicsConfiguration of a screen device.
void addNotify() 
Makes this Window displayable by creating the connection to its native screen resource.
void addPropertyChangeListener(PropertyChangeListener listener) 
Adds a PropertyChangeListener to the listener list.
void add Property ChangeListener(String property Name, Property Change Listener listener) 
Adds a PropertyChangeListener to the listener list for a specific property.
void addWindowFocusListener(WindowFocusListener l) 
Adds the specified window focus listener to receive window events from this window.
void addWindowListener(WindowListener l) 
Adds the specified window listener to receive window events from this window.
void addWindowStateListener(WindowStateListener l) 
Adds the specified window state listener to receive window events from this window.
void applyResourceBundle(ResourceBundle rb) 
Deprecated. As of J2SE 1.4, replaced by Component.applyComponentOrientation.
void applyResourceBundle(String rbName) 
Deprecated. As of J2SE 1.4, replaced by Component.applyComponentOrientation.
void createBufferStrategy(int numBuffers) 
Creates a new strategy for multi-buffering on this component.
void createBufferStrategy(int numBuffers, BufferCapabilities caps) 
Creates a new strategy for multi-buffering on this component with the required buffer capabilities.
void dispose() 
Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children.
AccessibleContext getAccessibleContext() 
Gets the AccessibleContext associated with this Window.
BufferStrategy	getBufferStrategy() 
Returns the BufferStrategy used by this component.
boolean getFocusableWindowState() 
Returns whether this Window can become the focused Window if it meets the other requirements outlined in isFocusableWindow.
Container getFocusCycleRootAncestor() 
Always returns null because Windows have no ancestors; they represent the top of the Component hierarchy.
Component getFocusOwner() 
Returns the child Component of this Window that has focus if this Window is focused; returns null otherwise.
Set<AWTKeyStroke> getFocusTraversalKeys(int id) 
Gets a focus traversal key for this Window.
GraphicsConfiguration	getGraphicsConfiguration() 
This method returns the GraphicsConfiguration used by this Window.
List<Image> getIconImages() 
Returns the sequence of images to be displayed as the icon for this window.
InputContext getInputContext() 
Gets the input context for this window.
<T extends EventListener> T[] getListeners(Class<T> listenerType) 
Returns an array of all the objects currently registered as FooListeners upon this Window.
Locale	getLocale() 
Gets the Locale object that is associated with this window, if the locale has been set.
Dialog.ModalExclusionType	getModalExclusionType() 
 Returns the modal exclusion type of this window.
Component getMostRecentFocusOwner() 
Returns the child Component of this Window that will receive the focus when this Window is focused.
Window[] getOwnedWindows() 
Return an array containing all the windows this window currently owns.
Window	getOwner() 
Returns the owner of this window.
static Window[]	getOwnerlessWindows() 
Returns an array of all Windows created by this application that have no owner.
Toolkit getToolkit() 
Returns the toolkit of this frame.
String	getWarningString() 
Gets the warning string that is displayed with this window.
WindowFocusListener[] getWindowFocusListeners() 
Returns an array of all the window focus listeners registered on this window.
WindowListener[] getWindowListeners() 
Returns an array of all the window listeners registered on this window.
static Window[] getWindows() 
Returns an array of all Windows, both owned and ownerless, created by this application.
WindowStateListener[] getWindowStateListeners() 
Returns an array of all the window state listeners registered on this window.
void hide() 
Deprecated. As of JDK version 1.5, replaced by setVisible(boolean).
boolean isActive() 
Returns whether this Window is active.
boolean isAlwaysOnTop() 
Returns whether this window is an always-on-top window.
boolean isAlwaysOnTopSupported() 
Returns whether the always-on-top mode is supported for this window.
boolean isFocusableWindow() 
Returns whether this Window can become the focused Window, that is, whether this Window or any of its subcomponents can become the focus owner.
boolean isFocusCycleRoot() 
Always returns true because all Windows must be roots of a focus traversal cycle.
boolean isFocused() 
Returns whether this Window is focused.
boolean isLocationByPlatform() 
Returns true if this Window will appear at the default location for the native windowing system the next time this Window is made visible.
boolean isShowing() 
Checks if this Window is showing on screen.
void pack() 
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.
void paint(Graphics g) 
Paints the container.
boolean postEvent(Event e) 
Deprecated. As of JDK version 1.1 replaced by dispatchEvent(AWTEvent).
protected void processEvent(AWTEvent e) 
Processes events on this window.
protected void processWindowEvent(WindowEvent e) 
Processes window events occurring on this window by dispatching them to any registered WindowListener objects.
protected void processWindowFocusEvent(WindowEvent e) 
Processes window focus event occuring on this window by dispatching them to any registered WindowFocusListener objects.
protected void processWindowStateEvent(WindowEvent e) 
Processes window state event occuring on this window by dispatching them to any registered WindowStateListener objects.
void removeNotify() 
Makes this Container undisplayable by removing its connection to its native screen resource.
void removeWindowFocusListener(WindowFocusListener l) 
Removes the specified window focus listener so that it no longer receives window events from this window.
void removeWindowListener(WindowListener l) 
Removes the specified window listener so that it no longer receives window events from this window.
void removeWindowStateListener(WindowStateListener l) 
Removes the specified window state listener so that it no longer receives window events from this window.
void reshape(int x, int y, int width, int height) 
Deprecated. As of JDK version 1.1, replaced by setBounds(int, int, int, int).
void setAlwaysOnTop(boolean alwaysOnTop) 
Sets whether this window should always be above other windows.
void setBounds(int x, int y, int width, int height) 
Moves and resizes this component.
void setBounds(Rectangle r) 
Moves and resizes this component to conform to the new bounding rectangle r.
void setCursor(Cursor cursor) 
Set the cursor image to a specified cursor.
void setFocusableWindowState(boolean focusableWindowState) 
Sets whether this Window can become the focused Window if it meets the other requirements outlined in isFocusableWindow.
void setFocusCycleRoot(boolean focusCycleRoot) 
Does nothing because Windows must always be roots of a focus traversal cycle.
void setIconImage(Image image) 
Sets the image to be displayed as the icon for this window.
void setIconImages(List<? extends Image> icons) 
Sets the sequence of images to be displayed as the icon for this window.
void setLocationByPlatform(boolean locationByPlatform) 
Sets whether this Window should appear at the default location for the native windowing system or at the current location (returned by getLocation) the next time the Window is made visible.
void setLocationRelativeTo(Component c) 
 Sets the location of the window relative to the specified component.
void setMinimumSize(Dimension minimumSize) 
Sets the minimum size of this window to a constant value.
void setModalExclusionType(Dialog.ModalExclusionType exclusionType) 
Specifies the modal exclusion type for this window.
void setSize(Dimension d) 
Resizes this component so that it has width d.width and height d.height.
void setSize(int width, int height) 
Resizes this component so that it has width width and height height.
void setVisible(boolean b) 
Shows or hides this Window depending on the value of parameter b.
void show() 
Deprecated. As of JDK version 1.5, replaced by setVisible(boolean).
void toBack() 
If this Window is visible, sends this Window to the back and may cause it to lose focus or activation if it is the focused or active Window.
void toFront() 
If this Window is visible, brings this Window to the front and may make it the focused Window.
This class inherits methods from the following classes:
java.awt.Window
java.awt.Window
java.awt.Container
java.awt.Container
java.awt.Component
java.awt.Component
java.lang.Object
java.lang.Object
Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtContainerDemo {
   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;
   private Label msglabel;

   public AwtContainerDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtContainerDemo  awtContainerDemo = new AwtContainerDemo();          
      awtContainerDemo.showFrameDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);
   
      msglabel = new Label();
      msglabel.setAlignment(Label.CENTER);
      msglabel.setText("Welcome to TutorialsPoint AWT Tutorial.");

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   
   private void showWindowDemo(){
      headerLabel.setText("Container in action: Window");   
      final MessageWindow window = 
         new MessageWindow(mainFrame,
         "Welcome to TutorialsPoint AWT Tutorial.");

      Button okButton = new Button("Open a Window");
      okButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            window.setVisible(true);
            statusLabel.setText("A Window shown to the user.");                
         }
      });
      controlPanel.add(okButton);
      mainFrame.setVisible(true);  
   }

   class MessageWindow extends Window{
      private String message; 

      public MessageWindow(Frame parent, String message) { 
         super(parent);               
         this.message = message; 
         setSize(300, 300);       
         setLocationRelativeTo(parent);
         setBackground(Color.gray);
      }

      public void paint(Graphics g) { 
         super.paint(g);
         g.drawRect(0,0,getSize().width - 1,getSize().height - 1); 
         g.drawString(message,50,150); 
      } 
   }
}
Compile the program using command prompt. Go to D:/ > AWT  and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtContainerDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtContainerDemo
Verify the following output

    13 Lectures 
     2 hours 

 EduOLC
 Print
 Add Notes
 Bookmark this page