SWING: Containers JPanel Class Cheat Sheet

Following is the declaration for javax.swing.JPanel class

public class JPanel
   extends JComponent
      implements Accessible

# Constructors

JPanel()Creates a new JPanel with a double buffer and a flow layout.
JPanel(boolean isDoubleBuffered)Creates a new JPanel with FlowLayout and the specified buffering strategy.
JPanel(LayoutManager layout)Creates a new buffered JPanel with the specified layout manager.
JPanel(LayoutManager layout, boolean isDoubleBuffered)Creates a new JPanel with the specified layout manager and buffering strategy.

# Methods

AccessibleContext getAccessibleContext()Gets the AccessibleContext associated with this JPanel.
PanelUI getUI()Returns the look and feel (L&F) object that renders this component.
String getUIClassID()Returns a string that specifies the name of the L&F class which renders this component.
protected String paramString()Returns a string representation of this JPanel.
void setUI(PanelUI ui)Sets the look and feel (L&F) object that renders this component.
void updateUI()Resets the UI property with a value from the current look and feel.

# Methods Inherited

  • javax.swing.JComponent
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

# JPanel Example

SwingContainerDemo.java

package com.simplecheatsheet.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("Vi du Java Swing");
      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("Chao mung ban den voi bai huong dan Java Swing.", 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);      
   }   
}

Leave a Reply

Your email address will not be published. Required fields are marked *