
- SWING Tutorial
- SWING - Home
- SWING - Overview
- SWING - Environment
- SWING - Controls
- SWING - Event Handling
- SWING - Event Classes
- SWING - Event Listeners
- SWING - Event Adapters
- SWING - Layouts
- SWING - Menu
- SWING - Containers
- SWING Useful Resources
- SWING - Quick Guide
- SWING - Useful Resources
- SWING - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
SWING - JPanel Class
Introduction
The class JPanel is a generic lightweight container.
Class Declaration
Following is the declaration for javax.swing.JPanel class −
public class JPanel extends JComponent implements Accessible
Class Constructors
Sr.No. | Constructor & Description |
---|---|
1 |
JPanel() Creates a new JPanel with a double buffer and a flow layout. |
2 |
JPanel(boolean isDoubleBuffered) Creates a new JPanel with FlowLayout and the specified buffering strategy. |
3 |
JPanel(LayoutManager layout) Creates a new buffered JPanel with the specified layout manager. |
4 |
JPanel(LayoutManager layout, boolean isDoubleBuffered) Creates a new JPanel with the specified layout manager and buffering strategy. |
Class Methods
Sr.No. | Method & Description |
---|---|
1 |
AccessibleContext getAccessibleContext() Gets the AccessibleContext associated with this JPanel. |
2 |
PanelUI getUI() Returns the look and feel (L&F) object that renders this component. |
3 |
String getUIClassID() Returns a string that specifies the name of the L&F class which renders this component. |
4 |
protected String paramString() Returns a string representation of this JPanel. |
5 |
void setUI(PanelUI ui) Sets the look and feel (L&F) object that renders this component. |
6 |
void updateUI() Resets the UI property with a value from the current look and feel. |
Methods Inherited
This class inherits methods from the following classes −
- javax.swing.JComponent
- java.awt.Container
- java.awt.Component
- java.lang.Object
JPanel Example
Create the following Java program using any editor of your choice in say D:/ > SWING > com > tutorialspoint > gui >
SwingContainerDemo.java
package com.tutorialspoint.gui; 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 TutorialsPoint 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 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); } }
Compile the program using the command prompt. Go to D:/ > SWING and type the following command.
D:\SWING>javac com\tutorialspoint\gui\SwingContainerDemo.java
If no error occurs, it means the compilation is successful. Run the program using the following command.
D:\SWING>java com.tutorialspoint.gui.SwingContainerDemo
Verify the following output.
