Showing posts with label layout. Show all posts
Showing posts with label layout. Show all posts

Set JPanel layout using BoxLayout

Complete source code below will show you how to set JPanel layout using BoxLayout

*******************************************************************
COMPLETE SOURCE CODE FOR : SetJPanelLayoutUsingBoxLayout.java
*******************************************************************


import javax.swing.*;

import java.awt.*;

public class SetJPanelLayoutUsingBoxLayout
{
public static void main(String[]args)
{
//Create a window using JFrame with title ( Set JPanel layout using BoxLayout )
JFrame frame=new JFrame("Set JPanel layout using BoxLayout");

//Create a panel using JPanel
JPanel panel=new JPanel();

//Set JPanel layout using BoxLayout with vertical lay out
//You can try change to BoxLayout.X_AXIS to make components lay out in horizontal
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));

//Create a button with text ( First button )
JButton button=new JButton("First button");

//Create a button with text ( Second button )
JButton button2=new JButton("Second button");

//Create a button with text ( Third button )
JButton button3=new JButton("Third button");

//Create a dimension with :
//Width : 10 pixels
//Height : 10 pixels
Dimension dim=new Dimension(10,10);

//Create an invisible box with dimension size and add into created panel
//You can try eliminate this and see what happen
panel.add(Box.createRigidArea(dim));

//Add button into panel
panel.add(button);

//Create an invisible box with dimension size and add into created panel
//You can try eliminate this and see what happen
panel.add(Box.createRigidArea(dim));

//Add button2 into panel
panel.add(button2);

//Create an invisible box with dimension size and add into created panel
//You can try eliminate this and see what happen
panel.add(Box.createRigidArea(dim));

//Add button3 into panel
panel.add(button3);

//Create an invisible box with dimension size and add into created panel
//You can try eliminate this and see what happen
panel.add(Box.createRigidArea(dim));

//add panel into created JFrame
frame.add(panel);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(500,200);

//Make JFrame locate at center of screen
frame.setLocationRelativeTo(null);

//Make JFrame visible
frame.setVisible(true);
}
}


*******************************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************************

Set JPanel layout using GridBagLayout

Source code below will show you, how to set JPanel layout using GridBagLayout.

**************************************************************
COMPLETE SOURCE CODE FOR : SetJPanelLayoutToGridBagLayout.java
**************************************************************


import javax.swing.*;
import java.awt.*;

public class SetJPanelLayoutToGridBagLayout extends JPanel
{
/**Note**
*When it say, "It will take 4 grid for it's width"
*it mean it will use 4 box in grid and it's direction is to right
*
*When it say, "It will take 4 grid for it's height"
*it mean it will use 4 box in grid and it's direction is to bottom
*/

//Create GridBigLayout object
GridBagLayout gbl=new GridBagLayout();

//Create GridBagConstraints object
GridBagConstraints gbc=new GridBagConstraints();

//Create a label using JLabel with text ( Name : )
JLabel label=new JLabel("Name :");

//Create a label using JLabel with text ( Age : )
JLabel label2=new JLabel("Age :");

//Create a text field using JTextField with numbers of column equal to 12
JTextField textField=new JTextField(12);

//Create a text field using JTextField with numbers of column equal to 3
JTextField textField2=new JTextField(3);

//Create a button with text ( OK )
JButton button=new JButton("OK");

//Create a button with text ( Cancel )
JButton button2=new JButton("Cancel");

//Create a box with vertical layout
Box box1=new Box(BoxLayout.Y_AXIS);

//Create a box with horizontal layout
Box box2=new Box(BoxLayout.X_AXIS);

//Create a box with horizontal layout
Box box3=new Box(BoxLayout.X_AXIS);

//Create a box with vertical layout
Box box4=new Box(BoxLayout.Y_AXIS);

//Create a window using JFrame with title ( Set JPanel layout using GridBagLayout )
JFrame frame=new JFrame("Set JPanel layout using GridBagLayout");

//Constructor
public SetJPanelLayoutToGridBagLayout()
{
//Make text in label allign to right
label.setHorizontalAlignment(SwingConstants.RIGHT);
label2.setHorizontalAlignment(SwingConstants.RIGHT);

//Set panel layout to GridBagLayout
setLayout(gbl);

//Field that will be use when display area is larger than component size.
//It mean when it has some space between component, component will
//enlarge to horizontal direction
gbc.fill=GridBagConstraints.HORIZONTAL;

/*
*Add label into panel with top left corner of the label at crossing between
*first horizontal and first vertical line.
*It will take 4 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(label,1,1,4,1);

/*
*Add label2 into panel with top left corner of the
*label at crossing between
*third horizontal and first vertical line.
*It will take 4 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(label2,3,1,4,1);

/*
*Add textField into panel with top left corner of the text field at
*crossing between first horizontal and sixth vertical line
*It will take 12 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(textField,1,6,12,1);

/*
*Add textField2 into panel with top left corner of the text field at
*crossing between third horizontal and sixth vertical line
*It will take 3 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(textField2,3,6,3,1);

/*
*Add button into panel with top left corner of the button at
*crossing between fifth horizontal and fifth vertical line
*It will take 5 grid for it's width
*It will take 2 grid for it's height
*/
addComponent(button,5,5,5,2);

/*
*Add button2 into panel with top left corner of the button at
*crossing between fifth horizontal and eleventh vertical line
*It will take 5 grid for it's width
*It will take 2 grid for it's height
*/
addComponent(button2,5,11,5,2);

/*
*Add a component that is invisible with 10 pixels width
*and 10 pixels height into box1
*/
box1.add(Box.createRigidArea(new Dimension(10,10)));

/*
*Add box1 into panel with top left corner of the box at
*crossing between first horizontal and fifth vertical line
*It will take 1 grid for it's width
*It will take 3 grid for it's height
*/
addComponent(box1,1,5,1,3);

/*
*Add a component that is invisible with 30 pixels width
*and 10 pixels height into box2
*/
box2.add(Box.createRigidArea(new Dimension(30,10)));

/*
*Add box2 into panel with top left corner of the box at
*crossing between second horizontal and first vertical line
*It will take 17 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(box2,2,1,17,1);

/*
*Add a component that is invisible with 30 pixels width
*and 10 pixels height into box3
*/
box3.add(Box.createRigidArea(new Dimension(30,10)));

/*
*Add box3 into panel with top left corner of the box at
*crossing between fourth horizontal and first vertical line
*It will take 17 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(box3,4,1,17,1);

/*
*Add a component that is invisible with 10 pixels width
*and 10 pixels height into box4
*/
box4.add(Box.createRigidArea(new Dimension(10,10)));

/*
*Add box4 into panel with top left corner of the box at
*crossing between fifth horizontal and tenth vertical line
*It will take 1 grid for it's width
*It will take 2 grid for it's height
*/
addComponent(box4,5,10,1,2);

//Add panel into JFrame
frame.add(this);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//SetJFrame size
frame.setSize(500,400);

//Make JFrame center on the screen
frame.setLocationRelativeTo(null);

//Disble JFrame from resizable
frame.setResizable(false);
frame.setVisible(true);
}

//Method addComponent
public void addComponent(Component component, int row, int column, int width, int height)
{
gbc.gridx=column;
gbc.gridy=row;
gbc.gridwidth=width;
gbc.gridheight=height;
gbl.setConstraints(component,gbc);
add(component);
}

//Main method
public static void main(String[]args)
{
SetJPanelLayoutToGridBagLayout sjpltgbl=new SetJPanelLayoutToGridBagLayout();
}
}


**************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************

Set JFrame layout using Grid Layout

Source code below will show you, how to set JFrame layout using GridLayout. GridLayout that we use in this source code contains 3 rows and 2 columns. When you use GridLayout to set layout, you just need to play with number of row and number of column. After that GridLayout will add your component from left to right. If it reached at last column in a row, it will go to the next row, and it will start again add component from left to right.

***************************************************************************
COMPLETE SOURCE CODE FOR : GridLayoutForJFrame.java
***************************************************************************


import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.GridLayout;

public class GridLayoutForJFrame
{
public static void main(String[]args)
{
//Create a JFrame with title ( Set JFrame layout using Grid Layout )
JFrame frame=new JFrame("Set JFrame layout using Grid Layout");

//Create grid layout that contains :
//3 rows
//2 columns
GridLayout layout=new GridLayout(3,2);

//Set JFrame layout to grid layout with 3 rows and 2 columns
frame.setLayout(layout);

//Create 6 buttons that we want to add into JFrame
JButton button1=new JButton("BUTTON1");
JButton button2=new JButton("BUTTON2");
JButton button3=new JButton("BUTTON3");
JButton button4=new JButton("BUTTON4");
JButton button5=new JButton("BUTTON5");
JButton button6=new JButton("BUTTON6");


//Add all buttons into JFrame

//Add first button at row number 1 and column number 1
frame.add(button1);

//Add second button at row number 1 and column number 2
frame.add(button2);

//Add third button at row number 2 and column number 1
frame.add(button3);

//Add fourth button at row number 2 and column number 2
frame.add(button4);

//Add fifth button at row number 3 and column number 1
frame.add(button5);

//Add sixth button at row number 3 and column number 2
frame.add(button6);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size :
//Width : 400 pixels
//Height : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


***************************************************************************
JUST COMPILE AND EXECUTE IT
***************************************************************************

Set JFrame layout using Border Layout

Source code below will show you how to add component into JFrame that use Border Layout as it's Layout Manager. If you use Border Layout, you must know 5 region that will be use by Border Layout to arrange component in a JFrame:

NORTH

WEST

CENTER

EAST

SOUTH



You cannot change this setting. If you want to add a component, you also must set location of your component. For example, if you want your component locate at SOUTH, you must use BorderLayout.SOUTH. You can see it in complete source code below. Don't forget to set your location.

********************************************************************************
COMPLETE SOURCE CODE FOR : BorderLayoutForJFrame.java
********************************************************************************


import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.BorderLayout;

public class BorderLayoutForJFrame
{
public static void main(String[]args)
{
//Create a JFrame with title ( Border Layout for JFrame layout manager )
JFrame frame=new JFrame("Border Layout for JFrame layout manager");

//Set JFrame layout to Border Layout
frame.setLayout(new BorderLayout());

//Create 5 buttons that we want to add into JFrame
JButton button1=new JButton("NORTH");
JButton button2=new JButton("WEST");
JButton button3=new JButton("CENTER");
JButton button4=new JButton("EAST");
JButton button5=new JButton("SOUTH");

//Add all buttons into JFrame
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.WEST);
frame.add(button3,BorderLayout.CENTER);
frame.add(button4,BorderLayout.EAST);
frame.add(button5,BorderLayout.SOUTH);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(600,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


********************************************************************************
JUST COMPILE AND EXECUTE IT
********************************************************************************

Set JFrame layout using Flow Layout

Source code below will show you how to set JFrame layout using Flow Layout. For your information, when you use Flow Layout to set your JFrame layout, component will be add from left to right. If there are no space, next component will be add at next line from left to right.

***************************************************************************
COMPLETE SOURCE CODE FOR : FlowLayoutForJFrame.java
***************************************************************************


import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.FlowLayout;

public class FlowLayoutForJFrame
{
public static void main(String[]args)
{
//Create a JFrame with title ( Set JFrame layout manager using Flow Layout )
JFrame frame=new JFrame("Set JFrame layout manager using Flow Layout");

//Set JFrame layout to Flow Layout
frame.setLayout(new FlowLayout());

//Create 6 buttons that want to add into JFrame
JButton button1=new JButton("BUTTON 1");
JButton button2=new JButton("BUTTON 2");
JButton button3=new JButton("BUTTON 3");
JButton button4=new JButton("BUTTON 4");
JButton button5=new JButton("BUTTON 5");
JButton button6=new JButton("BUTTON 6");

//Add all button into JFrame
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size with
//Width = 600 pixels
//Height = 400 pixels
frame.setSize(600,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


***************************************************************************
JUST COMPILE AND EXECUTE IT
***************************************************************************

RELAXING NATURE VIDEO