Java Swing | JWindow with examples
JWindow is a part of Java Swing and it can appear on any part of the users desktop. It is different from JFrame in the respect that JWindow does not have a title bar or window management buttons like minimize, maximize, and close, which JFrame has. JWindow can contain several components such as buttons and labels.
Constructor of the class are:
- JWindow() : creates an empty Window without any specified owner
- JWindow(Frame o) :creates an empty Window with a specified frame as its owner
- JWindow(Frame o) : creates an empty Window with a specified frame as its owner
- JWindow(Window o) : creates an empty Window with a specified window as its owner
- JWindow(Window o, GraphicsConfiguration g) : creates an empty window with a specified window as its owner and specified graphics Configuration.
- JWindow(GraphicsConfiguration g) :creates an empty window with a specified Graphics Configuration g.
Commonly used methods
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
- setLayout(LayoutManager m) : sets the layout of the Window to specified layout manager
- setContentPane(Container c) : sets the ContentPane property of the window
- getContentPane() : get the container which is the ContentPane for this Window
- add(Component c): adds component to the Window
- isVisible(boolean b): sets the visibility of the Window, if value of the boolean is true then visible else invisible
- update(Graphics g) : calls the paint(g) function
- remove(Component c) : removes the component c
- getGraphics() : returns the graphics context of the component.
- getLayeredPane() : returns the layered pane for the window
- setContentPane(Container c) :sets the content pane for the window
- setLayeredPane(JLayeredPane l) : set the layered pane for the window
- setRootPane(JRootPane r) : sets the rootPane for the window
- setTransferHandler(TransferHandler n) : Sets the transferHandler property, which is a mechanism to support transfer of data into this component.
- setRootPaneCheckingEnabled(boolean enabled) : Sets whether calls to add and setLayout are forwarded to the contentPane.
- setRootPane(JRootPane root) :Sets the rootPane property of the window.
- setGlassPane(Component glass) : Sets the glassPane property of the window.
- repaint(long time, int x, int y, int width, int height): Repaints the specified rectangle of this component within time milliseconds.
- remove(Component c): Removes the specified component from the window.
- isRootPaneCheckingEnabled() : Returns whether calls to add and setLayout are forwarded to the contentPane or not .
- getTransferHandler() : returns the transferHandler property.
- getRootPane() : Returns the rootPane object for this window.
- getGlassPane() : Returns the glassPane object for this window.
- createRootPane() : Called by the constructor methods to create the default rootPane.
- addImpl(Component co, Object c, int i) : Adds the specified child Component to the window.
The following programs will illustrate the use of JWindow
1. program to create a simple JWindow
// java Program to create a simple JWindowimport java.awt.event.*;import java.awt.*;import javax.swing.*; class solveit extends JFrame implements ActionListener { // frame static JFrame f; // main class public static void main(String[] args) { // create a new frame f = new JFrame("frame"); // create a object solveit s = new solveit(); // create a panel JPanel p = new JPanel(); JButton b = new JButton("click"); // add actionlistener to button b.addActionListener(s); // add button to panel p.add(b); f.add(p); // set the size of frame f.setSize(400, 400); f.show(); } // if button is pressed public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); if (s.equals("click")) { // create a window JWindow w = new JWindow(f); // set panel JPanel p = new JPanel(); // create a label JLabel l = new JLabel("this is a window"); // set border p.setBorder(BorderFactory.createLineBorder(Color.black)); p.add(l); w.add(p); // set background p.setBackground(Color.red); // setsize of window w.setSize(200, 100); // set visibility of window w.setVisible(true); // set location of window w.setLocation(100, 100); } }} |
Output :
1. program to create a multiple JWindow .( where one window is the owner of the other )
// java program to create a multiple JWindow .( where one window is the owner of the other )<import java.awt.event.*;import java.awt.*;import javax.swing.*;class solveit extends JFrame implements ActionListener { // frame static JFrame f; // windows JWindow w, w1; // object of class static solveit s; // main class public static void main(String[] args) { // create a new frame f = new JFrame("frame"); // create a object s = new solveit(); // create a panel JPanel p = new JPanel(); JButton b = new JButton("click"); // add actionlistener to button b.addActionListener(s); // add button to panel p.add(b); f.add(p); // set the size of frame f.setSize(400, 400); f.show(); } // if button is pressed public void actionPerformed(ActionEvent e) { String s1 = e.getActionCommand(); if (s1.equals("click")) { // create a window w = new JWindow(f); // set panel JPanel p = new JPanel(); // create a label JLabel l = new JLabel("this is first window"); // create a button JButton b = new JButton("Click me"); // add Action listener b.addActionListener(s); // set border p.setBorder(BorderFactory.createLineBorder(Color.black)); p.add(l); p.add(b); w.add(p); // set background p.setBackground(Color.red); // setsize of window w.setSize(200, 100); // set visibility of window w.setVisible(true); // set location of window w.setLocation(100, 100); } else { // create a window w1 = new JWindow(w); // set panel JPanel p = new JPanel(); // create a label JLabel l = new JLabel("this is the second window"); // set border p.setBorder(BorderFactory.createLineBorder(Color.black)); p.add(l); w1.add(p); // set background p.setBackground(Color.blue); // setsize of window w1.setSize(200, 100); // set visibility of window w1.setVisible(true); // set location of window w1.setLocation(210, 210); } }} |
Output :
Note : the above programs might nor run in an online compiler please use an offline IDE


