Java Swing - JPanel With Examples Last Updated : 10 Nov, 2021 Comments Improve Suggest changes Like Article Like Report JPanel, a part of the 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 organization of components, however, it does not have a title bar. Constructors of JPanel JPanel(): creates a new panel with a flow layoutJPanel(LayoutManager l): creates a new JPanel with specified layoutManagerJPanel(boolean isDoubleBuffered): creates a new JPanel with a specified buffering strategyJPanel(LayoutManager l, boolean isDoubleBuffered): creates a new JPanel with specified layoutManager and a specified buffering strategyCommonly used Functions of JPanel add(Component c): Adds a component to a specified containersetLayout(LayoutManager l): sets the layout of the container to the specified layout managerupdateUI(): resets the UI property with a value from the current look and feel.setUI(PanelUI ui): sets the look and feel of an 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. Let us take a sample program in order to illustrate the use of JPanel class by appending sequential execution snapshots of outputs justifying the below program sets as follows: Example: Java // Java Program to Create a Simple JPanel // and Adding Components to it // Importing required classes import java.awt.*; import java.awt.event.*; import javax.swing.*; // Class 1 // Helper class extending JFrame class class solution extends JFrame { // JFrame static JFrame f; // JButton static JButton b, b1, b2; // Label to display text static JLabel l; // Main class public static void main(String[] args) { // Creating a new frame to store text field and // button f = new JFrame("panel"); // Creating a label to display text l = new JLabel("panel label"); // Creating a new buttons b = new JButton("button1"); b1 = new JButton("button2"); b2 = new JButton("button3"); // Creating a panel to add buttons JPanel p = new JPanel(); // Adding buttons and textfield to panel // using add() method p.add(b); p.add(b1); p.add(b2); p.add(l); // setbackground of panel p.setBackground(Color.red); // Adding panel to frame f.add(p); // Setting the size of frame f.setSize(300, 300); f.show(); } } Output: Example 2: Java // Java Program to Create a JPanel with a Border Layout // and Adding Components to It // Importing required classes import java.awt.*; import java.awt.event.*; import javax.swing.*; // Main class // Extending JFrame class class solution extends JFrame { // JFrame static JFrame f; // JButton static JButton b, b1, b2, b3; // Label to display text static JLabel l; // Main driver method public static void main(String[] args) { // Creating a new frame to store text field and // button f = new JFrame("panel"); // Creating a label to display text l = new JLabel("panel label"); // Creating a new buttons b = new JButton("button1"); b1 = new JButton("button2"); b2 = new JButton("button3"); b3 = new JButton("button4"); // Creating a panel to add buttons // and a specific layout JPanel p = new JPanel(new BorderLayout()); // Adding buttons and textfield to panel // using add() method p.add(b, BorderLayout.NORTH); p.add(b1, BorderLayout.SOUTH); p.add(b2, BorderLayout.EAST); p.add(b3, BorderLayout.WEST); p.add(l, BorderLayout.CENTER); // setbackground of panel p.setBackground(Color.red); // Adding panel to frame f.add(p); // Setting the size of frame f.setSize(300, 300); f.show(); } } Output: Example 3: Java // Java Program to Create a JPanel with a Box layout // and Adding components to it // Importing required classes import java.awt.*; import java.awt.event.*; import javax.swing.*; // Main class // Extending JFrame class class solution extends JFrame { // JFrame static JFrame f; // JButton static JButton b, b1, b2, b3; // Label to display text static JLabel l; // Main drive method public static void main(String[] args) { // Creating a new frame to store text field and // button f = new JFrame("panel"); // Creating a label to display text l = new JLabel("panel label"); // Creating a new buttons b = new JButton("button1"); b1 = new JButton("button2"); b2 = new JButton("button3"); b3 = new JButton("button4"); // Creating a panel to add buttons and // textfield and a layout JPanel p = new JPanel(); // Setting box layout p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); // Adding buttons and textfield to panel p.add(b); p.add(b1); p.add(b2); p.add(b3); p.add(l); // Setting background of panel p.setBackground(Color.red); // Adding panel to frame f.add(p); // Setting the size of frame f.setSize(300, 300); f.show(); } } Output: Henceforth, we are successfully able to generate buttons in our panel. 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. Comment More infoAdvertise with us Next Article Java Swing - JPanel With Examples andrew1234 Follow Improve Article Tags : Java Programming Language java-swing Practice Tags : Java Similar Reads Java Swing | JSplitPane with Examples JSplitPane is a part of Java Swing. JSplitPane is used to divide only two components. JSplitPane is to use resize the components . By using the JSplitPane the user can manually resize the component till its minimum size . JSplitPane can be of two types, one is the vertical and horizontal splitpane C 6 min read Java Swing | JDialog with examples JDialog is a part Java swing package. The main purpose of the dialog is to add components to it. JDialog can be customized according to user need .Constructor of the class are: JDialog() : creates an empty dialog without any title or any specified ownerJDialog(Frame o) :creates an empty dialog with 5 min read Java Swing | JList with examples JList is part of Java Swing package . JList is a component that displays a set of Objects and allows the user to select one or more items . JList inherits JComponent class. JList is a easy way to display an array of Vectors .Constructor for JList are : JList(): creates an empty blank listJList(E [ ] 4 min read 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 butt 5 min read Java Swing | JCheckBox with examples JCheckBox is a part of Java Swing package . JCheckBox can be selected or deselected . It displays it state to the user . JCheckBox is an implementation to checkbox . JCheckBox inherits JToggleButton class. Constructor of the class are : JCheckBox() : creates a new checkbox with no text or icon JChec 5 min read Java Swing | JComboBox with examples JComboBox is a part of Java Swing package. JComboBox inherits JComponent class . JComboBox shows a popup menu that shows a list and the user can select a option from that specified list . JComboBox can be editable or read- only depending on the choice of the programmer .Constructor of the JComboBox 7 min read Java Swing | Internal Frame with examples JInternalFrame is a part of Java Swing . JInternalFrame is a container that provides many features of a frame which includes displaying title, opening, closing, resizing, support for menu bar, etc. Constructors for JInternalFrame JInternalFrame() : creates a new non- closable, non- resizable, non- i 8 min read Java Swing | Popup and PopupFactory with examples Popup and PopupFactory are a part of the Java Swing library. Popups are used when we want to display to the user a Component on the top of all the other Components in that particular containment hierarchy. PopupFactory is the class that is used to create popups. Popups have a very small life cycle, 5 min read Spring Boot JpaRepository with Example Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se 9 min read JLabel | Java Swing JLabel is a class of java Swing . JLabel is used to display a short string or an image icon. JLabel can display text, image or both . JLabel is only a display of text or image and it cannot get focus . JLabel is inactive to input events such a mouse focus or keyboard focus. By default labels are ver 4 min read Like