SWING - JPanel Class


Advertisements

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

S.N. 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)

Create 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

S.N. 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 that 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 command prompt. Go to D:/ > SWING and type the following command.

D:\SWING>javac com\tutorialspoint\gui\SwingContainerDemo.java

If no error comes that means compilation is successful. Run the program using following command.

D:\SWING>java com.tutorialspoint.gui.SwingContainerDemo

Verify the following output

SWING JPanel
swing_containers.htm

Advertisements