Monday, April 16, 2012

Specify JPanel Size using the Class Dimension and the method setPreferredSize();

Design Free Website with Free Internet Marketing Tools

Program Description:

This is just a simple and short java program on how to set or customize JPanel size using the class Dimension and setPreferredSize(); method.

The program is very easy to understand and one of the important thing you need to remember is that customizing JPanel size depends on what layout you are going to use. In this program I am using FlowLayout because with FlowLayout, all the JComponents being added to the container are free in terms of length, width, and height meaning you can manipulate its size property easily unlike GridLayout, BoxLayout, CardLayout, and so on where the JComponent are being forced to fit in a certain size meaning JComponent size property manipulation is disabled.

So I hope this simple code can help. 100% runnable.

Output:
Code:

/**
 * File: jpanelSize.java
 * Tiltle: Specify JPanel Size
 * Author: http://java-program-sample.blogspot.com/
 */

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;

public class jpanelSize extends JFrame {
 
 //Initializing JPanel
 JPanel panel1 = new JPanel();
 JPanel panel2 = new JPanel();
 JPanel panel3 = new JPanel();
 JPanel panel4 = new JPanel();

 //Setting up GUI
    public jpanelSize() {
     
     //Setting up the Title of the Window
     super("Specify JPanel Size");

     //Set Size of the Window (WIDTH, HEIGHT)
     setSize(570,250);

     //Exit Property of the Window
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     //Setting up the container ready for the components to be added.
     Container pane = getContentPane();
     setContentPane(pane);
     
     //Setting the layout of the container
     pane.setLayout(new FlowLayout(FlowLayout.CENTER));
  
  //Creating different JPanel sizes using the class Dimension and the method setPreferredSize()
     panel1.setBorder(BorderFactory.createLineBorder(Color.black, 1));
     panel1.setPreferredSize(new Dimension(200,200));
     panel1.setBackground(Color.BLUE);
     
     panel2.setBorder(BorderFactory.createLineBorder(Color.black, 1));
     panel2.setPreferredSize(new Dimension(150,150));
     panel2.setBackground(Color.CYAN);
     
     panel3.setBorder(BorderFactory.createLineBorder(Color.black, 1));
     panel3.setPreferredSize(new Dimension(100,100));
     panel3.setBackground(Color.RED);
     
     panel4.setBorder(BorderFactory.createLineBorder(Color.black, 1));
     panel4.setPreferredSize(new Dimension(50,50));
     panel4.setBackground(Color.GREEN);
     
     //Adding all four JPanel in the container
     pane.add(panel1);
     pane.add(panel2);
     pane.add(panel3);
     pane.add(panel4);

     /**Set all the Components Visible.
      * If it is set to "false", the components in the container will not be visible.
      */
     setVisible(true);
    }
    
 //Main Method
    public static void main (String[] args) {
     jpanelSize js = new jpanelSize();
 }
}

Important Part of the Program:

//Creating different JPanel sizes using the class Dimension and the method setPreferredSize()
     panel1.setBorder(BorderFactory.createLineBorder(Color.black, 1));
     panel1.setPreferredSize(new Dimension(200,200));
     panel1.setBackground(Color.BLUE);
     
     panel2.setBorder(BorderFactory.createLineBorder(Color.black, 1));
     panel2.setPreferredSize(new Dimension(150,150));
     panel2.setBackground(Color.CYAN);
     
     panel3.setBorder(BorderFactory.createLineBorder(Color.black, 1));
     panel3.setPreferredSize(new Dimension(100,100));
     panel3.setBackground(Color.RED);
     
     panel4.setBorder(BorderFactory.createLineBorder(Color.black, 1));
     panel4.setPreferredSize(new Dimension(50,50));
     panel4.setBackground(Color.GREEN);

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hostgator Discount Code