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
- 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
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solveit extends JFrame implements ActionListener {
static JFrame f;
public static void main(String[] args)
{
f = new JFrame( "frame" );
solveit s = new solveit();
JPanel p = new JPanel();
JButton b = new JButton( "click" );
b.addActionListener(s);
p.add(b);
f.add(p);
f.setSize( 400 , 400 );
f.show();
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals( "click" )) {
JWindow w = new JWindow(f);
JPanel p = new JPanel();
JLabel l = new JLabel( "this is a window" );
p.setBorder(BorderFactory.createLineBorder(Color.black));
p.add(l);
w.add(p);
p.setBackground(Color.red);
w.setSize( 200 , 100 );
w.setVisible( true );
w.setLocation( 100 , 100 );
}
}
}
|
Output :
1. 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 {
static JFrame f;
JWindow w, w1;
static solveit s;
public static void main(String[] args)
{
f = new JFrame( "frame" );
s = new solveit();
JPanel p = new JPanel();
JButton b = new JButton( "click" );
b.addActionListener(s);
p.add(b);
f.add(p);
f.setSize( 400 , 400 );
f.show();
}
public void actionPerformed(ActionEvent e)
{
String s1 = e.getActionCommand();
if (s1.equals( "click" )) {
w = new JWindow(f);
JPanel p = new JPanel();
JLabel l = new JLabel( "this is first window" );
JButton b = new JButton( "Click me" );
b.addActionListener(s);
p.setBorder(BorderFactory.createLineBorder(Color.black));
p.add(l);
p.add(b);
w.add(p);
p.setBackground(Color.red);
w.setSize( 200 , 100 );
w.setVisible( true );
w.setLocation( 100 , 100 );
}
else {
w1 = new JWindow(w);
JPanel p = new JPanel();
JLabel l = new JLabel( "this is the second window" );
p.setBorder(BorderFactory.createLineBorder(Color.black));
p.add(l);
w1.add(p);
p.setBackground(Color.blue);
w1.setSize( 200 , 100 );
w1.setVisible( true );
w1.setLocation( 210 , 210 );
}
}
}
|
Output :
Note : the above programs might nor run in an online compiler please use an offline IDE
Last Updated :
06 Jun, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...