Java Swing | JWindow with examples - GeeksforGeeks

06 Jun, 2018
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 ownerJWindow(Frame o) :creates an empty Window with a specified frame as its ownerJWindow(Frame o) : creates an empty Window with a specified frame as its ownerJWindow(Window o) : creates an empty Window with a specified window as its ownerJWindow(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.
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
setLayout(LayoutManager m) : sets the layout of the Window to specified layout managersetContentPane(Container c) : sets the ContentPane property of the windowgetContentPane() : get the container which is the ContentPane for this Windowadd(Component c): adds component to the WindowisVisible(boolean b): sets the visibility of the Window, if value of the boolean is true then visible else invisibleupdate(Graphics g) : calls the paint(g) functionremove(Component c) : removes the component cgetGraphics() : returns the graphics context of the component.getLayeredPane() : returns the layered pane for the windowsetContentPane(Container c) :sets the content pane for the windowsetLayeredPane(JLayeredPane l) : set the layered pane for the windowsetRootPane(JRootPane r) : sets the rootPane for the windowsetTransferHandler(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.
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
java-swing
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Introduction to Java
Internal Working of HashMap in Java
Comparator Interface in Java with Examples
Strings in Java