SWING JWindow Class

Advertisements


Introduction

The class JWindow is a container that can be displayed but does not have the title bar or window-management buttons.

Class declaration

Following is the declaration for javax.swing.JWindow class:

public class JWindow
   extends Window
      implements Accessible, RootPaneContainer

Field

Following are the fields for java.awt.Component class:

  • protected AccessibleContext accessibleContext --The accessible context property.

  • protected JRootPane rootPane --The JRootPane instance that manages the contentPane and optional menuBar for this frame, as well as the glassPane.

  • protected boolean rootPaneCheckingEnabled --If true then calls to add and setLayout will be forwarded to the contentPane.

Class constructors

S.N.Constructor & Description
1JWindow()
Creates a window with no specified owner.
2JWindow(Frame owner)
Creates a window with the specified owner frame.
3JWindow(GraphicsConfiguration gc)
Creates a window with the specified GraphicsConfiguration of a screen device.
4JWindow(Window owner)
Creates a window with the specified owner window.
5JWindow(Window owner, GraphicsConfiguration gc)
Creates a window with the specified owner window and GraphicsConfiguration of a screen device.

Class methods

S.N.Method & Description
1protected void addImpl(Component comp, Object constraints, int index)
Adds the specified child Component.
2protected JRootPane createRootPane()
Called by the constructor methods to create the default rootPane.
3AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JWindow.
4Container getContentPane()
Returns the Container which is the contentPane for this window.
5Component getGlassPane()
Returns the glassPane Component for this window.
6Graphics getGraphics()
Creates a graphics context for this component.
7JLayeredPane getLayeredPane()
Returns the layeredPane object for this window.
8JRootPane getRootPane()
Returns the rootPane object for this window.
9TransferHandler getTransferHandler()
Gets the transferHandler property.
10protected boolean isRootPaneCheckingEnabled()
Returns whether calls to add and setLayout are forwarded to the contentPane.
11protected String paramString()
Returns a string representation of this JWindow.
12void remove(Component comp)
Removes the specified component from the container.
13void repaint(long time, int x, int y, int width, int height)
Repaints the specified rectangle of this component within time milliseconds.
14void setContentPane(Container contentPane)
Sets the contentPane property for this window.
15void setGlassPane(Component glassPane)
Sets the glassPane property.
16void setLayeredPane(JLayeredPane layeredPane)
Sets the layeredPane property.
17void setLayout(LayoutManager manager)
Sets the LayoutManager.
18protected void setRootPane(JRootPane root)
Sets the new rootPane object for this window.
19protected void setRootPaneCheckingEnabled(boolean enabled)
Sets whether calls to add and setLayout are forwarded to the contentPane.
20void setTransferHandler(TransferHandler newHandler)
Sets the transferHandler property, which is a mechanism to support transfer of data into this component.
21void update(Graphics g)
Calls paint(g).
22protected void windowInit()
Called by the constructors to init the JWindow properly.

Methods inherited

This class inherits methods from the following classes:

  • java.awt.Window

  • java.awt.Container

  • java.awt.Component

  • java.lang.Object

JWindow Example

Create the following java program using any editor of your choice in say D:/ > SWING > com > tutorialspoint > gui >

SwingContainerDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingContainerDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
   private JLabel msglabel;

   public SwingContainerDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      SwingContainerDemo  swingContainerDemo = new SwingContainerDemo();  
      swingContainerDemo.showJWindowDemo();
   }

   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing 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 JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    

      statusLabel.setSize(350,100);

      msglabel = new JLabel("Welcome to TutorialsPoint SWING Tutorial."
         , JLabel.CENTER);

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

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showJWindowDemo(){
      headerLabel.setText("Container in action: JWindow");   
      final MessageWindow window = 
         new MessageWindow(mainFrame, 
         "Welcome to TutorialsPoint SWING Tutorial.");

      JButton okButton = new JButton("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 JWindow{
      private String message; 

      public MessageWindow(JFrame parent, String 
         message) { 
         super(parent);               
         this.message = message; 
         setSize(300, 300);       
         setLocationRelativeTo(parent);         
      }

      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:/ > SWING and type the following command.

D:\SWING>javac com\tutorialspoint\gui\SwingContainerDemo.java

If no error comes that means compilation is successful. Run the program using following command.

D:\SWING>java com.tutorialspoint.gui.SwingContainerDemo

Verify the following output

SWING JWindow

Advertisements
Advertisements