Gui Layout Manager In Java

GUI - LAYOUT MANAGERS :

The layout manager are a set of classes that implement the java.AWT.LayoutManager interface and help to position the components in a container. The interface takes a task of laying out the child components in the container. The task is achieved by resizing and moving the child components. The advantages of this type of mechanism is that when the container is resized the layout manager automatically updates the interface

The basic layout managers includes :

1) FlowLayout :

It is a simple layout manager that works like a word processor. It is also the default Layout manager for the panel. The flow layout lays out components linewise from left to right.

FlowLaout can be created using following constructors

  • a. FlowLaout() : Constructs a new layout with centered alignment, leaving a vertical and horizontal gap.
  • b. FlowLayout(int aling, int vgap, int hgap) : Constructs a new flowlayout with the alignment specified, leaving a vertical and horizontal gap as specified.

Various methods can be used alog with the flow layout. For eg. getAlignment(), getHgap(), getAlignment(int align) etc.

CODE/PROGRAM/EXAMPLE
//Example for Flow Layout
import java.awt.*;
import java.awt.event.*;
class FlowDemo extends Frame
{
Button b1 = new Button("one");
Button b2 = new Button("two");
public FlowDemo(String s)
{
super(s);
setSize(400,400);
setLayout(new FlowLayout(FlowLayout.LEFT));
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
add(b1);
add(b2);
}
public static void main(String arg[])
{
Frame f=new Frame();
f.show();
}
}

Save the file as FlowDemo. Java

Compile the file using javac FlowDemo.java

On successful compilation, execute the file using java FlowDemo.java

2) Grid Layout :

It lays out components in a way very similar to spred sheet (rows and columns). Specifying the number of rows and columns in grid creates the Grid layout.

Grid Layout can be created using following constructors

  • a. GridLayout() : Creates a grid layout with a default of one column per component in a single row.
  • b. GridLayout(int rows, int cols, int hgap, int vgap) : Creates a grid layout with the specified rows and columns and specified horizontal and vertical gaps.

Various methods can be used alog with the Grid layout. For eg. getColumns(), getRows(), geHgap(), getVgap() etc.

CODE/PROGRAM/EXAMPLE
//Example for Grid Layout
import java.applet.Applet;
import java.awt.*;
public class Grid1 extends Applet {
LayoutManager Layout;
Button [ ] Buttons;
public Grid1 () {
int i;
Layout = new GridLayout (3, 2);
setLayout (Layout);
Buttons = new Button [5];
for (i = 0; i < 5; ++i) {
Buttons[i] = new Button ();
Buttons[i].setLabel ("Button " + (i + 1));
add (Buttons[i]);
}
}
}

Save the file as Grid1. Java

Compile the file using javac Grid1.java

On successful compilation, execute the file using appletviewer Grid1.java

The output appers as shown in following figure :

applet grid outout in java

3) BorderLayout :

It is the class that enables specification, i.e. where on the border of container each component should be placed. All areas need not be filled. The size of the areas will depend on the components they contain.

Border Layout can be created using following constructors

  • a. BorderLayout() : It creates a new border layout with no gap between the components.
  • b. BorderLayout(int hgap, int vgap) : It creates a border layout with the specified horizontal and vertical gap between components.

Various methods can be used alog with the Border layout. For eg. getHgap(), getVgap(), setHgap(int hgap), setVgap(int vgap)

CODE/PROGRAM/EXAMPLE
//Example for Border Layout
import java.awt.*;
import java.applet.*;
import java.util.*;
/*<applet code="BorderDemo" width=300 height=100>
</applet>
*/
public class BorderDemo extends Applet
{
public void init()
{
setLayout(new BorderLayout());
add(new Button("This across the top"),BorderLayout.NORTH);
add(new Button("The Footer message might go
here"),BorderLayout.SOUTH);
add(new Button("Right"),BorderLayout.EAST);
add(new Button("Left"),BorderLayout.WEST);
String msg=" This is border layout";
add(new TextArea(msg),BorderLayout.CENTER);
add(new Button("new"),BorderLayout.CENTER);
}
}

Save the file as BorderDemo. Java

Compile the file using javac BorderDemo.java

On successful compilation, execute the file using appletviewer BorderDemo.java

The output appers as shown in following figure :

applet Border Layout in java

METHODS OF AWT :

The common methods of AWT components are as follow :

getLocation () -

This method is used to get position of the component, as a Point. The usage of the method is shown below.

Syntax
//Syntax
Point p = someComponent.getLocation ();
int x = p.x;
int y = p.y;

the x and y parts of the location can be easily accessed by using getX () and getY (). It is always efficient to use getX () and getY () methods.

For example,
int x = someComponent.getX();
int y = someComponent.getY();

getLocationOnScreen () -

This method is used to get the position of the upper-left corner of the screen of the component, as a Point. The usage of the method is shown below.

Syntax
//Syntax
Point p = someComponent.getLocationOnScreen ();
int x = p.x;
int y = p.y;

It is always advisable to use getLocation () method (if working on Java 2 platform).

getBounds () -

This method is used to get the current bounding Rectangle of component. The usage of the method is shown below.

Syntax
//Syntax
Rectangle r = someComponent.getBounds ();
int height = r.height;
int width = r.width;
int x = r.x;
int y = r.y;

if you need a Rectangle object then the efficient way is to use getX (), getY(), getWidth(), and getHeight() methods.

getSize () -

This method is used to get the current size of component, as a Dimension. The usage of the method is shown below.

Syntax
//Syntax
Dimension d = someComponent.getSize ();
int height = d.height;
int width = d.width;

use getWidth () and getHeight () methods to directly access the width and height. You can also use getSize () if you require a Dimension object.

For Example,
int height = someComponent.getHeight();
int width = someComponent.getWidth();

setBackground(Color)/setForeground(Color) -

This method is used to change the background/foreground colors of the component

setFont (Font) -

This method is used to change the font of text within a component.

setVisible (boolean) -

This method is used for the visibility state of the component. The component appears on the screen if setVisible () is set to true and if it’s set to false then the component will not appear on the screen. Furthermore, if we mark the component as not visible then the component will disappear while reserving its space in the GUI.

setEnabled (boolean) -

This method is used to toggle the state of the component. The component will appear if set to true and it will also react to the user. ON the contrary, if set to false then the component will not appear hence no user interaction will be there.

As discussed earlier a container is a component that can be nested. The most widely used Panel is the Class Panel which can be extended further to partition GUIs. There is a Panel which is used for running the programs. This Panel is known as Class Applet which is used for running the programs within the Browser.

Common Container Methods :-

All the subclasses of the Container class inherit the behavior of more than 50 common methods of Container. These subclasses of the container mostly override the method of component. Some of the methods of container which are most widely used are as follow :

  • getComponents ();
  • add();
  • getComponentCount();
  • getComponent(int);

ScrollPane :-

The ScrollPane container provides an automatic scrolling of any larger component introduced with the 1.1 release of the Java Runtime Environment (JRE). Any image which is bigger in size for the display area or a bunch of spreadsheet cells is considered as a large object. Moreover there is no LayoutManager for a ScrollPane because only a single object exists within it. However, the mechanism of Event Handling is being managed for scrolling.

The example below shows the Scrollpane.

This scrollpane demonstrates the scrolling of the large image. In the program code below, first of all we have created a scrollpane by creating its object, and then we have passed the parameter of image in it. We have also set the border layout as centre, as shown.

CODE/PROGRAM/EXAMPLE
//Example for Scroll Pane :
import java.awt.*;
import java.applet.*;
/*<applet code="ScrollingImageDemo" width=200 height=100>
</applet>
*/
class Scrollpane extends Component {
private Image image;
public Scrollpane(Image m)
{
image = m;
}
public void paint(Graphics g)
{
if (image != null)
g.drawImage(image, 0, 0, this);
}
}
public class ScrollingImageDemo extends Applet
{
public void init()
{
setLayout(new BorderLayout());
ScrollPane SC = new ScrollPane(ScrollPane.SCROLLBARS_AL
WAYS);
Image mg = getImage(getCodeBase(), "cute-puppy.gif");
SC.add(new Scrollpane(mg));
add(SC, BorderLayout.CENTER);
}
}

Save the file as ScrollingImageDemo. Java

Compile the file using javac ScrollingImageDemo.java

On successful compilation, execute the file using appletviewer ScrollingImageDemo.java

The output appers as shown in following figure :

java Scrolling Image code output
#gui_layout_manager_in_java #Flow_Layout_program_in_java #methods_of_awt_using_java #Common_Container_Methods_in_awt #ScrollPane_in_java

(New page will open, for Comment)

Not yet commented...