Graphical User Interface In Java

GRAPHICAL USER INTERFACE (GUI) :

INTRODUCTION :

A type of user interface item that allows people to interact with programs in more ways than typing such as computers and many hand-held devices such as mobile phones is called a graphical user interface (GUI) . A GUI offers graphical icons, and visual indicators, as opposed to text-based interfaces. This helps to develop more efficient programs that are easy to work with. The user can interact with the application without any problem.

The GUI application is created in three steps. These are :

  • Add components to Container objects to make your GUI.
  • Then you need to setup event handlers for the user interaction with GUI.
  • Explicitly display the GUI for application.

GUI COMPONENTS :

It is visual object and the user interacts with this object via a mouse or a keyboard. Components included, can be actually seen on the screen, such as, buttons, labels etc. Any operation that is common to all GUI components are found in class Component. Different components are available in the Java AWT (Abstract Window Toolkit )package for developing user interface for your program.

A class library is provided by the Java programming language which is known as Abstract Window Toolkit (AWT). The Abstract Window Toolkit (AWT) contains several graphical widgets which can be added and positioned to the display area with a layout manager.

AWT is a powerful concept in JAVA. AWT is basically used to develop for GUI application building. AWT is platform dependant. That means your .class file after the program compilation is platform independent but the look of your GUI application is platform dependant. AWT copies GUI component from local macines operating system. That means your applications look will differ in MAC operating system, as you have seen in WINDOWS operating system.

INTERFACE AND CLASSES OF AWT PACKAGE :

Some of the Classes Interfaces of AWT package are explained below

Interfaces Descriptions
ActionEvent This interface is used for handling events.
Adjustable This interface takes numeric value to adjust within the bounded range.
Composite This interface defines methods to draw a graphical area. It combines a shape, text, or image etc.
CompositeContext This interface allows the existence of several contexts simultaneously for a single composite object. It handles the state of the operations.
ItemSelectable This interface is used for maintaining zero or more selection for items from the item list.
KeyEventDispatcher The KeyEventDispatcher implements the current KeyboardFocusManager and it receives KeyEvents before dispatching their targets.
KeyEventPostProcessor This interface also implements the current KeyboardFocusManager. The KeyboardFocusManager receives the KeyEvents after that dispatching their targets.
LayoutManager It defines the interface class and it has layout containers.
LayoutManager2 This is the interface extends from the LayoutManager and is subinterface of that.
MenuContainer This interface has all menu containers.
Paint This interface is used to color pattern. It used for the Graphics2D operations.
PaintContext This interface also used the color pattern. It provides an important color for the Graphics2D operation and uses the ColorModel.
PaintGraphics This interface provides print a graphics context for a page.
Shape This interface used for represent the geometric shapes.
Stroke This interface allows the Graphics2D object and contains the shapes to outline or stylistic representation of outline.
Transparency This interface defines the transparency mode for implementing classes.

Class hierarchy of AWT classes can be given as follows.

Class hierarchy of AWT classes

Some of the AWT components are explained below :

Labels :

This is the simplest component of Java Abstract Window Toolkit. This component is generally used to show the text or string in your application and label never perform any type of action.

Syntax for defining the label only and with justification :

Syntax
//Syntax
Label label_name = new Label ("This is the label text.");

above code simply represents the text for the label.

CODE/PROGRAM/EXAMPLE
Label label_name = new Label ("This is the label text. ” ,abel.CENTER);

Justification of label can be left, right or centered. Above declaration used the center justification of the label using the Label.CENTER.

CODE/PROGRAM/EXAMPLE
//Example for Label.
import java.awt.*;
import java.applet.Applet;
/*<applet code="LabelTest" width=200 height=100>
</applet>
*/
public class LabelTest extends Applet
{
public void init()
{
add(new Label("A label"));
// right justify next label
add(new Label("Another label", Label.RIGHT));
}
}

Save the file as LabelTest. Java

Compile the file using javac LabelTest.java

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

The output appers as shown in following figure :

applet label output

Buttons :

This is the component of Java Abstract Window Toolkit and is used to trigger actions and other events required for your application. The syntax of defining the button is as follows : Button button_name = new Button ("This is the label of the button.");

You can change the Button's label or get the label's text by using the Button.setLabel (String) and Button.getLabel () method. Buttons are added to its container using the, add (button_name) method.

CODE/PROGRAM/EXAMPLE
//Example for Buttons :-
import java.awt.*;
import java.applet.Applet;
/*<applet code="ButtonTest" width=200 height=100>
</applet>
* /
public class ButtonTest extends Applet
{
public void init()
{
Button button = new Button ("OK");
add (button);
}
}

Save the file as ButtonTest.Java

Compile the file using javac ButtonTest.java

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

The output appers as shown in following figure :

applet button output in java

Note that in the above example there is no event handling added; pressing the button will not do anything.

Check Boxes :

This component of Java AWT allows you to create check boxes in your applications. The syntax of the definition of Checkbox is as follows :

Syntax
Checkbox checkbox_name = new Checkbox ("Optional check box 1", false);

Above code constructs the unchecked Checkbox by passing the boolean valued argument false with the Checkbox label through the Checkbox() constructor. Defined Checkbox is added to its container using add (checkbox_name) method. You can change and get the checkbox's label using the setLabel (String) and getLabel () method. You can also set and get the state of the checkbox using the setState (boolean) and getState () method provided by the Checkbox class.

CODE/PROGRAM/EXAMPLE
//Example for Check Boxes :-
import java.awt.*;
import java.applet.Applet;
/*<applet code="CheckboxTest" width=200 height=100>
</applet>
*
public class CheckboxTest extends Applet
{
public void init()
{
Checkbox m = new Checkbox ("Allow Mixed Case");
add (m);
}
}

Save the file as CheckboxTest. Java

Compile the file using javac CheckboxTest.java

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

The output appers as shown in following figure :

applet checkbox output in java

Radio Button :

Radio buttons are a bunch of option boxes in a group. Only one of then can be checked at a time. This is useful if you need to give the user a few options where only one will apply. This is the special case of the Checkbox component of Java AWT package. This is used as a group of checkboxes whos group name is same. Only one Checkbox from a Checkbox Group can be selected at a time.

Syntax for creating radio buttons is as follows :

Syntax
//Syntax
CheckboxGroup chkboxgp = new CheckboxGroup ();
add (new Checkbox ("chkboxname", chkboxgp, value);

“Value” in the second statement can only be true or false.If you mention more than one true valued for checkboxes then your program takes the last true and shows the last check box as checked.

CODE/PROGRAM/EXAMPLE
//Example for Radio Buttons.
import java.awt.*;
import java.applet.Applet;
/*<applet code="Rbutton" width=200 height=100>
</applet>
*/
public class Rbutton extends Applet
{
public void init()
{
CheckboxGroup chkgp = new CheckboxGroup ();
add (new Checkbox ("One", chkgp, false));
add (new Checkbox ("Two", chkgp, false));
add (new Checkbox ("Three",chkgp, false));
}
}

In the above code we are making three check boxes with the label "One", "Two" and "Three".

Save the file as Rbutton. Java

Compile the file using javac Rbutton.java

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

The output appers as shown in following figure :

applet button output in java

Text Area :

This is the text container component of Java AWT package. The Text Area contains plain text. TextArea can be declared as follows :

Syntax
//Syntax
TextArea txtArea_name = new TextArea ();

You can make the Text Area editable or not using the setEditable (boolean) method. If you pass the boolean valued argument false then the text area will be non-editable otherwise it will be editable. The text area is by default in editable mode. Texts are set in the text area using the setText (string) method of the TextArea class.

CODE/PROGRAM/EXAMPLE
//Example for Text Area :-
import java.awt.*;
import java.applet.Applet;
/*<applet code="TAreaTest" width=200 height=100>
</applet>
*/
public class TAreaTest extends Applet
{
TextArea disp;
public void init()
{
disp = new TextArea("Code goes here", 10, 30);
add (disp);
}
}

Save the file as TAreaTest. Java

Compile the file using javac TAreaTest.java

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

The output appers as shown in following figure :

applet textarea output in java

Text Field :

This is also the text container component of Java AWT package. This component contains single line and limited text information. This is declared as follows :

Syntax
//Syntax
TextField txtfield = new TextField (20);

You can fix the number of columns in the text field by specifying the number in the constructor. In the above code we have fixed the number of columns to 20.

A displayed label object is known as the Label. Most of the times label is used to demonstrate the significance of the other parts of the GUI. It helps to display the functioning of the next text field. A label is also restricted to a single line of text as a button.

CODE/PROGRAM/EXAMPLE
//Example for Text Field:-
import java.awt.*;
import java.applet.Applet;
/*<applet code="TFieldTest" width=200 height=100>
</applet>
*/
public class TFieldTest extends Applet
{
public void init()
{
TextField f1 =
new TextField("type something");
add(f1);
}
}

Save the file as TFieldTest.java

Compile the file using javac TFieldTest.java

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

The output appers as shown in following figure :

applet text field in java

Scrollbar :

Scrollbar is represented by a "slider" widget. The characteristics of it are specified by integer values which are being set at the time of scrollbar construction. Both the types of Sliders are available i.e. horizontal and vertical.

The example below shows the code for the scrollbar construction. The subtraction of scrollbar width from the maximum setting gives the maximum value of the Scrollbar. In the program code, '0' is the <<<<<<< scrollbar.shtml initial value of the scrollbar, '8' is the width of the scrollbar.

CODE/PROGRAM/EXAMPLE
//Example for Scrollbar
import java.applet.Applet;
/*<applet code="ScrollbarDemo" width=200 height=100>
</applet>
*/
public class ScrollbarDemo extends Applet
{
public void init()
{
Scrollbar sb = new Scrollbar
(Scrollbar.VERTICAL, 0, 8, -100, 100);
add(sb);
}
}

Save the file as ScrollbarDemo.java

Compile the file using javac ScrollbarDemo.java

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

The output appers as shown in following figure :

applet Scrollbar in java

Panels :

A panel is an object which holds other objects. It’s just a container to organize and arrange your GUI better. Once, you learn about Layout Managers you’ll see why panels are a useful tool. For now, just know that they’re useful. Here’s an example of a set of buttons added into a panel :

Syntax
//Syntax
Panel myPanel = new Panel();
myPanel.add(helloButton);
myPanel.add(goodbyeButton);
add(myPanel);

It looks no different than if you just added the buttons regularly, but you’ll see why you might want to use panels later on... This is what it looks like :

applet Panels in java
#graphical_user_interface_in_java #graphical_user_interface_in_java_language #gui_in_java #gui_in_java_language #gui_components #interface_and_class_of_awt_package #hierarchy_of_AWT_classes #Buttons_using_awt #Check_Boxes_using_awt #Radio_Button_using_awt #Text_Area_using_awt #Text_Field_using_awt #Scrollbar__using_awt #Panels__using_awt

(New page will open, for Comment)

Not yet commented...