Java Swing JWindow Example
A JWindow
is a container that can be displayed anywhere on the user’s desktop. It does not have the title bar, window-management buttons, or other trimmings associated with a JFrame
, but it is still a “first-class citizen” of the user’s desktop, and can exist anywhere on it.
1. Introduction
The JWindow
component contains a JRootPane
as its only child. The contentPane
should be the parent of any children of the JWindow
. As a convenience add and its variants, remove and setLayout
have been overridden to forward to the contentPane as necessary. This means you can write:
JwindowExample.java
window.add(child);
And the child will be added to the contentPane. The contentPane will always be non-null. Attempting to set it to null will cause the JWindow to throw an exception. The default contentPane will have a BorderLayout manager set on it.
Like a JFrame
, a JWindow
is another top-level container. It is as an undecorated JFrame
. It does not have features like a title bar, windows menu, etc. It is not a very commonly used top-level container. You can use it as a splash window that displays once when the application is launched and then automatically disappears after a few seconds.
1.1 Setup
Popular Java Editors:
To write your java programs you will need a text editor. There are even more sophisticated IDE available in the market. But for now, you can consider one of the following:
- Notepad: On Windows machine you can use any simple text editor like Notepad TextPad.
- NetBeans: is a Java IDE that is open source and free which can be downloaded from http://www.netbeans.org/index.html.
- Eclipse: is also a java IDE developed by the eclipse open source community and can be downloaded from http://www.eclipse.org
Prerequisite
This example is developed on Eclipse therefore a compatible Eclipse IDE is required to be installed on the system.
We also need WindowBuilder tool to be installed on Eclipse IDE for the easiness of the work. To learn how to install WindowBuilder tool please visit the Setup section 2.1 of the following link click here.
2. Class Declaration
Following is the declaration for javax.swing.JWindow class:
JwindowExample.java
public class JWindow extends Window implements Accessible, RootPaneContainer
2.1 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.
2.2 Class constructors
- JWindow()
Creates a window with no specified owner. - JWindow(Frame owner)
Creates a window with the specified owner frame. - JWindow(GraphicsConfiguration gc)
Creates a window with the specified GraphicsConfiguration of a screen device. - JWindow(Window owner)
Creates a window with the specified owner window. - JWindow(Window owner, GraphicsConfiguration gc)
Creates a window with the specified owner window and GraphicsConfiguration of a screen device.
2.3 Class methods
- protected void addImpl(Component comp, Object constraints, int index)
Adds the specified child Component. - protected JRootPane createRootPane()
Called by the constructor methods to create the default rootPane. - AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JWindow. - Container getContentPane()
Returns the Container which is the contentPane for this window. - Component getGlassPane()
Returns the glassPane Component for this window. - Graphics getGraphics()
Creates a graphics context for this component. - JLayeredPane getLayeredPane()
Returns the layeredPane object for this window. - JRootPane getRootPane()
Returns the rootPane object for this window. - TransferHandler getTransferHandler()
Gets the transferHandler property - protected boolean isRootPaneCheckingEnabled()
Returns whether calls to add and setLayout are forwarded to the contentPane. - protected String paramString()
Returns a string representation of this JWindow. - void remove(Component comp)
Removes the specified component from the container. - void repaint(long time, int x, int y, int width, int height)
Repaints the specified rectangle of this component within time milliseconds. - void setContentPane(Container contentPane)
Sets the contentPane property for this window. - void setGlassPane(Component glassPane)
Sets the glassPane property. - void setLayeredPane(JLayeredPane layeredPane)
Sets the layeredPane property. - void setLayout(LayoutManager manager)
Sets the LayoutManager. - protected void setRootPane(JRootPane root)
Sets the new rootPane object for this window. - protected void setRootPaneCheckingEnabled(boolean enabled)
Sets whether calls to add and setLayout are forwarded to the contentPane. - void setTransferHandler(TransferHandler newHandler)
Sets the transferHandler property, which is a mechanism to support transfer of data into this component. - void update(Graphics g)
Calls paint(g). - protected void windowInit()
Called by the constructors to init the JWindow properly.
2.4 Methods inherited
This class inherits methods from the following classes:
- java.awt.Window
- java.awt.Container
- java.awt.Component
- java.lang.Object
3. JWindow Example
Create the following java program using any editor of your choice
JwindowExample.java
package swing; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JwindowExample{ private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; private JLabel msglabel; public JwindowExample(){ prepareGUI(); } public static void main(String[] args){ JwindowExample JwindowExampledemo= new JwindowExample(); JwindowExampledemo.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 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 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); } } }
4. Output
The Output of the code when executed will look like the one below.
5. Download The Source Code
This was an example of creation of JAVA Window Example.
You can download the full source code of this example here: JwindowExample