Thursday, July 8, 2010

Border Layout  

0 comments

BorderLayout divides the container into five Geographical regions namely
North,South,East,West and Center.This is very commonly used Lyout
Manager.You can add at most one component to each region of a BorderLayout.
To put more than one component in a section, put them in a JPanel (with its own layout),
then add that panel to the border layout.
When you add components to a container which uses BorderLayout, specify the target region as the second parameter as, for example, BorderLayout.SOUTH.
If nothing is added to a region then neighbouring regions go on to fill up space.



Construcors:-
new BorderLayout();
new BorderLayout(int hgap,int vgap);

Example
Source Code:-
import javax.swing.*;
import java.awt.*;

class borderlayout extends JFrame{
public static void main(String a[]){
//initializing frames
JFrame jf=new JFrame("BorderLayout");
JFrame jf1=new JFrame("BorderLayout");
//initializing panels
JPanel panel=new JPanel();
//initializing buttons
JButton jbn=new JButton("North");
JButton jbs=new JButton("South");
JButton jbe=new JButton("East");
JButton jbw=new JButton("West");
JButton jbc=new JButton("Central");
//setting up layout
panel.setLayout(new BorderLayout());
panel.add(jbn,BorderLayout.NORTH);
panel.add(jbs,BorderLayout.SOUTH);
panel.add(jbe,BorderLayout.EAST);
panel.add(jbw,BorderLayout.WEST);
panel.add(jbc,BorderLayout.CENTER);
//adding to frame
jf.getContentPane().add(panel);
jf.setSize(250,200);
jf.setVisible(true);
}
}

Card Layout Manager  

0 comments

One of the most complex Layout Manager is Card Layout manager.Using this manager you can create a stack of cards and flip from one layout to another.This type of organization is similar to tabbed dialogs.Card Layout provides following 

Costructors
:-

public CardLayout(); //Constructor creates new CardLayout

public CardLayout(int hgap,int vgap);//Constructor creates a new card layout with specified horizontal and vertical gap.




To Create a Card Layout Manager with card layout manager object,you first create a parent panel to hold the cards .Then you create an object of the CardLayout class and set it as the layout manager of the panel.Finally ,you add each card to the layout by creating the component and adding the to the panel.

Following are the methods to switch between cards that are in stack of layout.

1.void first(Container parent) //Display first card
2.void last(Container parent) //Display last card
3.void next(Container parent) //Display next card
4.void previous(Container parent) //Display previous card
5.void first(Container parent,String name) //Display specified card

Example
code snippet:-

/*
CardLayout c1;
JPanel panel;
JButton jb 1;
cardlayout()
{
JFrame jf1=new JFrame("Card Layout demo");
JPanel panel=new JPanel();
JButton jb1=new JButton("Next Button>>");
JButton jb2=new JButton("Last Button>>");
CardLayout c1=new CardLayout();
panel.setLayout(c1);
panel.add("button1",jb1);
panel.add("button2",jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);
jf1.getContentPane().add(panel);
jf1.setVisible(true);
jf1.setSize(200,200);
}
public void actionPerformed(ActionEvent ae)
{
c1.next(panel);

}*/

Grid Bag Layout  

0 comments

The GridBag Layout manager is the most flexible and complex layout manager that the AWT provides. It places components in rows and columns allowing specific components by assigning weights to individual components in the gridbag layout.

When you specify the size and position of components,you also need to specify the constraint for each component.To specify constraint ,you need to set the variables in GridBagConstraints Object and specify theGridBagLayout manager object with the setConstraint() method, to associate the constraint with the component.

The GridBagLayout class has a single constructor that does not take any arguments.Since the position of each component in a layout is controlled by a GridBagLayout object and is determined by the currently setGridBagConstraints object,you need to create the GridBagConstraintsobject before the layout can be built.

GridBagLayout g=new GridBagLayout();
GridBagConstraints cons=new GridBagConstraints();




You can assign following values to GridBagConstraints attributes:-

anchor:-It is used when the component is smaller than its display area to determine where to place the compoenent.valid values are shown below:

GridBagConstraints.CENTER
GridBagConstraints.NORTH
GridBagConstraints.NORTHEAST
GridBagConstraints.EAST
GridBagConstraints.SOUTHEAST
GridBagConstraints.SOUTH
GridBagConstraints.SOUTHWEST
GridBagConstraints.WEST
GridBagConstraints.NORTHWEST

fill:- It is used when the display area of component is larger than the requested size of the component to determine whether to resize the component.valid values are:-
GridBagConstraints.NONE
GridBagConstraints.HORIZONTAL
GridBagConstraints.VERTICAL
GridBagConstraints.BOTH

gridwidth,gridheight:-It specifies number of columns or rows in display area of components the default value is 1,valid values are:
GridBagConstraints.REMAINDER
GridBagConstraints.RELATIVE

gridx,gridy:-They specify the row and column at the upper left of the components display area.The leftmost column has the address gridx=0 and topmost cell has the address gridy=0.

insets:- They specify the extenal padding of the component ,that is ,the minimum amount of space between the components and the edges of its display area.The value is specified as an Insets object .By default ,each component does not have external padding.The default padding value is insets(0,0,0,0).

ipadx,ipady:-They specify the internal padding to be added to the minimum size of the component.The default value is 0.The width of the component will be its minimum width plus ipadx*2 pixels.Similarly ,the height of the component will be its minimum height plus ipady*2 pixels.

weightx,weighty:- They determine whether the component stretch horizontally to fill the display area of the applet or vertically.The default value is 0 for both.



Example
Source code:-

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

public class GridBagWindow extends JFrame {
boolean inAnApplet = true;
final boolean shouldFill = true;
final boolean shouldWeightX = true;

public GridBagWindow() {
JButton button;
Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
contentPane.setLayout(gridbag);
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}

button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.gridx = 0;
c.gridy = 0;
gridbag.setConstraints(button, c);
contentPane.add(button);

button = new JButton("2");
c.gridx = 1;
c.gridy = 0;
gridbag.setConstraints(button, c);
contentPane.add(button);

button = new JButton("Button 3");
c.gridx = 2;
c.gridy = 0;
gridbag.setConstraints(button, c);
contentPane.add(button);

button = new JButton("Long-Named Button 4");
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
gridbag.setConstraints(button, c);
contentPane.add(button);

button = new JButton("Button 5");
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.SOUTH; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = 2; //third row
gridbag.setConstraints(button, c);
contentPane.add(button);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (inAnApplet) {
dispose();
} else {
System.exit(0);
}
}
});
}

public static void main(String args[]) {
GridBagWindow window = new GridBagWindow();
window.inAnApplet = false;

window.setTitle("GridBagLayout");
window.pack();
window.setVisible(true);
}
}

JButton  

0 comments


JButton
Jbutton is the standrad command button used in Java.It is mainly used for event driven programming.A JButton when clicked fires an event which is handled by an event handler which in response do some work as indicated in hadler.On click the JButton takes some set of input and process that.The JButton is a Swing Component.


JButton Class has following constructors:-

JButton() // Creates a button with no text or icon

JButton(Action a) //Creates a button where are properties are tak
en from action supplied

JButton(Icon icon)//Creates a button with a icon

JButton(String text)//Creates a button with some text

JButton(String text ,Icon icon)//Creates button with both text and icon

Features of JButton:-

1.JButton applies event handling.
2.JButton has a different look and feel for enabled and disabled buttons.
3.You can set mnemonics for JButtons.
4.TooltipText can also be set for JButtons.

Following example shows the application of all above mentioned features:-
In this example there are buttons which on click disable and enable the corresponding buttons.
The program follows:-
import javax.swing.*;
import java.awt.event.*;
public class ButtonDemo extends JPanel
implements ActionListener {
protected JButton but1, but2, but3;

public ButtonDemo() {
ImageIcon leftButtonIcon = createImageIcon("d:/image/left.gif");
ImageIcon middleButtonIcon = createImageIcon("c:/image/middle.gif");
ImageIcon rightButtonIcon = createImageIcon("c:/image/right.gif");

but1 = new JButton("Disable middle button", leftButtonIcon);
but1.setActionCommand("disable");
//setting up mnemonics
but1.setMnemonic(KeyEvent.VK_D);
but2 = new JButton("Middle button", middleButtonIcon);
but2.setMnemonic(KeyEvent.VK_M);

but3 = new JButton("Enable middle button", rightButtonIcon);
//Use the default text position of CENTER, TRAILING (RIGHT).
but3.setMnemonic(KeyEvent.VK_E);
but3.setActionCommand("enable");
//enabling the command button
but3.setEnabled(false);

//Listeners for actions on buttons 1 and 3.
but1.addActionListener(this);
but3.addActionListener(this);

but1.setToolTipText("Click this button to disable the middle button.");
but3.setToolTipText("Click this button to enable the middle button.");

//Add Components to this container, using the default FlowLayout.
add(but1);
add(but2);
add(but3);
}

public void actionPerformed(ActionEvent e) {
if ("disable".equals(e.getActionCommand())) {
but2.setEnabled(false);
but1.setEnabled(false);
but3.setEnabled(true);
} else {
but2.setEnabled(true);
but1.setEnabled(true);
but3.setEnabled(false);
}
}

/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = ButtonDemo.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}

// Create the GUI.
private static void createAndShowGUI() {

//Create and set up the JFrame.
JFrame frame = new JFrame("ButtonDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
ButtonDemo newContentPane = new ButtonDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

//Display the JFrame.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

Extension Factory Builder