JPanel, a part of Java Swing package, is a container that can store a group of components. The main task of JPanel is to organize components, various layouts can be set in JPanel which provide better organisation of components, however it does not have a title bar.
Constructor of JPanel are :
- JPanel() : creates a new panel with flow layout
- JPanel(LayoutManager l) : creates a new JPanel with specified layoutManager
- JPanel(boolean isDoubleBuffered) : creates a new JPanel with a specified buffering strategy
- JPanel(LayoutManager l, boolean isDoubleBuffered) : creates a new JPanel with specified layoutManager and a specified buffering strategy
Commonly used functions :
- add(Component c) : adds component to a specified container
- setLayout(LayoutManager l) : sets the layout of the container to specified layout manager
- updateUI() : resets the UI property with a value from the current look and feel.
- setUI(PanelUI ui) : sets the look and feel object that renders this component.
- getUI() : returns the look and feel object that renders this component.
- paramString() : returns a string representation of this JPanel.
- getUIClassID() : returns the name of the Look and feel class that renders this component.
- getAccessibleContext() : gets the AccessibleContext associated with this JPanel.
programs to illustrate use of JPanel
1. Program to create a simple JPanel add components to it
2. Program to create a JPanel with a Border layout and add components to it .
3. Program to create a JPanel with a Box layout and add components to it .
Note: In the previous Program, border layout and Box Layout are used. Different other layouts can be used to organize the components in a definite pattern, such as card layout, grid layout etc.
The above programs might not run in an online compiler please use an Offline IDE.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Java Swing | JList with examples
- Java Swing | JSplitPane with Examples
- Java Swing | JWindow with examples
- Java Swing | JSeparator with examples
- Java Swing | JDialog with examples
- Java Swing | JCheckBox with examples
- Java Swing | JComboBox with examples
- Java Swing | Popup and PopupFactory with examples
- Java Swing | Internal Frame with examples
- Java Swing | Translucent and shaped Window in Java
- Java Swing | JSlider
- Java Swing | JFileChooser
- Java Swing | JToolBar
- Java Swing | ToolTip
- Java Swing | MatteBorder
- JRadioButton | Java Swing
- Java Swing | Look and Feel
- Java Swing | JTextArea
- Java Swing | JSpinner
- Java Swing | JPopupMenu
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.