Java Swing | JPanel with examples

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 :

  1. JPanel() : creates a new panel with flow layout
  2. JPanel(LayoutManager l) : creates a new JPanel with specified layoutManager
  3. JPanel(boolean isDoubleBuffered) : creates a new JPanel with a specified buffering strategy
  4. JPanel(LayoutManager l, boolean isDoubleBuffered) : creates a new JPanel with specified layoutManager and a specified buffering strategy

Commonly used functions :



  1. add(Component c) : adds component to a specified container
  2. setLayout(LayoutManager l) : sets the layout of the container to specified layout manager
  3. updateUI() : resets the UI property with a value from the current look and feel.
  4. setUI(PanelUI ui) : sets the look and feel object that renders this component.
  5. getUI() : returns the look and feel object that renders this component.
  6. paramString() : returns a string representation of this JPanel.
  7. getUIClassID() : returns the name of the Look and feel class that renders this component.
  8. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// java Program to create a simple JPanel add components to it
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solution extends JFrame {
  
    // JFrame
    static JFrame f;
  
    // JButton
    static JButton b, b1, b2;
  
    // label to diaplay text
    static JLabel l;
  
    // main class
    public static void main(String[] args)
    {
        // create a new frame to stor text field and button
        f = new JFrame("panel");
  
        // create a label to display text
        l = new JLabel("panel label");
  
        // create a new buttons
        b = new JButton("button1");
        b1 = new JButton("button2");
        b2 = new JButton("button3");
  
        // create a panel to add buttons
        JPanel p = new JPanel();
  
        // add buttons and textfield to panel
        p.add(b);
        p.add(b1);
        p.add(b2);
        p.add(l);
  
        // setbackground of panel
        p.setBackground(Color.red);
  
        // add panel to frame
        f.add(p);
  
        // set the size of frame
        f.setSize(300, 300);
  
        f.show();
    }
}

chevron_right


Output :

2. Program to create a JPanel with a Border layout and add components to it .

filter_none

edit
close

play_arrow

link
brightness_4
code

// java Program to create a JPanel with a Border layout and add components to it .
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solution extends JFrame {
  
    // JFrame
    static JFrame f;
  
    // JButton
    static JButton b, b1, b2, b3;
  
    // label to diaplay text
    static JLabel l;
  
    // main class
    public static void main(String[] args)
    {
        // create a new frame to stor text field and button
        f = new JFrame("panel");
  
        // create a label to display text
        l = new JLabel("panel label");
  
        // create a new buttons
        b = new JButton("button1");
        b1 = new JButton("button2");
        b2 = new JButton("button3");
        b3 = new JButton("button4");
  
        // create a panel to add buttons and  a specific layout
        JPanel p = new JPanel(new BorderLayout());
  
        // add buttons and textfield to panel
        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);
  
        // add panel to frame
        f.add(p);
  
        // set the size of frame
        f.setSize(300, 300);
  
        f.show();
    }
}

chevron_right


Output :

3. Program to create a JPanel with a Box layout and add components to it .

filter_none

edit
close

play_arrow

link
brightness_4
code

// java Program to create a JPanel with a Box layout and add components to it .
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solutio extends JFrame {
  
    // JFrame
    static JFrame f;
  
    // JButton
    static JButton b, b1, b2, b3;
  
    // label to diaplay text
    static JLabel l;
  
    // main class
    public static void main(String[] args)
    {
        // create a new frame to stor text field and button
        f = new JFrame("panel");
  
        // create a label to display text
        l = new JLabel("panel label");
  
        // create a new buttons
        b = new JButton("button1");
        b1 = new JButton("button2");
        b2 = new JButton("button3");
        b3 = new JButton("button4");
  
        // create a panel to add buttons and textfield and a layout
        JPanel p = new JPanel();
  
        // set Box Layout
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
  
        // add buttons and textfield to panel
        p.add(b);
        p.add(b1);
        p.add(b2);
        p.add(b3);
        p.add(l);
  
        // setbackground of panel
        p.setBackground(Color.red);
  
        // add panel to frame
        f.add(p);
  
        // set the size of frame
        f.setSize(300, 300);
  
        f.show();
    }
}

chevron_right


Output :

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.



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.