Containers are an integral part of SWING GUI components. A container provides a space where a component can be located. A Container in AWT is a component itself and it provides the capability to add a component to itself. Following are certain noticable points to be considered.
Sub classes of Container are called as Container. For example, JPanel, JFrame and JWindow.
Container can add only a Component to itself.
A default layout is present in each container which can be overridden using setLayout method.
SWING Containers
Following is the list of commonly used containers while designing GUI using SWING.
Panel: JPanel is the simplest container. It provides space in which any other component can be placed, including other panels.
Frame: A JFrame is a top-level window with a title and a border.
Window: A JWindow object is a top-level window with no borders and no menubar.
Introduction-JPanel:
The class JPanel is a generic lightweight container.
Class Constructors:
JPanel(): Creates a new JPanel with a double buffer and a flow layout.
JPanel(boolean isDoubleBuffered): Creates a new JPanel with FlowLayout and the specified buffering strategy.
JPanel(LayoutManager layout): Creates a new buffered JPanel with the specified layout manager.
JPanel(LayoutManager layout, boolean isDoubleBuffered): Creates a new JPanel with the specified layout manager and buffering strategy.
Class Methods:
AccessibleContext getAccessibleContext(): Gets the AccessibleContext associated with this JPanel.
PanelUI getUI(): Returns the look and feel (L&F) object that renders this component.
String getUIClassID(): Returns a string that specifies the name of the L&F class which renders this component.
protected String paramString(): Returns a string representation of this JPanel.
void setUI(PanelUI ui): Sets the look and feel (L&F) object that renders this component.
void updateUI(): Resets the UI property with a value from the current look and feel.
JPanel Example:
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.showJPanelDemo();
}
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 CodersArts SWING blog", JLabel.CENTER);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showJPanelDemo(){
headerLabel.setText("Container in action: JPanel");
JPanel panel = new JPanel();
panel.setBackground(Color.magenta);
panel.setLayout(new FlowLayout());
panel.add(msglabel);
controlPanel.add(panel);
mainFrame.setVisible(true);
}
}
Output:
Introduction-JFrame:
The class JFrame is an extended version of java.awt.Frame that adds support for the JFC/Swing component architecture.
Field
Following are the fields for java.awt.Component class −
protected AccessibleContext accessibleContext −The accessible context property.
static int EXIT_ON_CLOSE − The exit application default window close operation.
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:
JFrame(): Constructs a new frame that is initially invisible.
JFrame(GraphicsConfiguration gc): Creates a Frame in the specified GraphicsConfiguration of a screen device and a blank title.
JFrame(String title): Creates a new, initially invisible Frame with the specified title.
JFrame(String title, GraphicsConfiguration gc): Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device.
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.
protected void frameInit(): Called by the constructors to init the JFrame properly.
AccessibleContext getAccessibleContext(): Gets the AccessibleContext associated with this JFrame.
Container getContentPane(): Returns the contentPane object for this frame.
int getDefaultCloseOperation(): Returns the operation that occurs when the user initiates a "close" on this frame.
Component getGlassPane(): Returns the glassPane object for this frame.
Graphics getGraphics(): Creates a graphics context for this component.
JMenuBar getJMenuBar(): Returns the menubar set on this frame.
JLayeredPane getLayeredPane(): Returns the layeredPane object for this frame.
JRootPane getRootPane(): Returns the rootPane object for this frame.
TransferHandler getTransferHandler(): Gets the transferHandler property.
static boolean isDefaultLookAndFeelDecorated(): Returns true if the newly created JFrames have their Window decorations provided by the current look and feel.
protected boolean isRootPaneCheckingEnabled(): Returns whether calls to add and setLayout are forwarded to the contentPane.
protected String paramString(): Returns a string representation of this JFrame.
protected void processWindowEvent(WindowEvent e): Processes window events occurring on this component.
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.
void setDefaultCloseOperation(int operation): Sets the operation that will happen by default when the user initiates a "close" on this frame.
static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated): Provides a hint as to whether or not newly created JFrames should have their Window decorations (such as borders, widgets to close the window, title...) provided by the current look and feel.
void setGlassPane(Component glassPane): Sets the glassPane property.
void setIconImage(Image image): Sets the image to be displayed as the icon for this window.
void setJMenuBar(JMenuBar menubar): Sets the menubar for this frame.
void setLayeredPane(JLayeredPane layeredPane): Sets the layeredPane property.
void setLayout(LayoutManager manager): Sets the LayoutManager.
protected void setRootPane(JRootPane root): Sets the rootPane property.
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 the transfer of data into this component.
void update(Graphics g): Just calls paint(g).
JFrame Example:
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.showJFrameDemo();
}
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 CodersArts.", JLabel.CENTER);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showJFrameDemo(){
headerLabel.setText("Container in action: JFrame");
final JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setLayout(new FlowLayout());
frame.add(msglabel);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
frame.dispose();
}
});
JButton okButton = new JButton("Open a Frame");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusLabel.setText("A Frame shown to the user.");
frame.setVisible(true);
}
});
controlPanel.add(okButton);
mainFrame.setVisible(true);
}
}
Output:
Introduction-JWindow
The class JWindow is a container that can be displayed but does not have the title bar or window-management buttons.
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
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.
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 the 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.
JWindow Example:
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);
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 CodersArts.");
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(400, 400);
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);
}
}
}
Output:
How does CodersArts help you in Java coding?
CodersArts provide :
Java swing assignment Help
Help in Java GUI development Projects
Mentorship from Experts Live 1:1 session
Course and Project completions
CourseWork help
Comments