Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

GUI Programming in Java: A Comprehensive Guide - Prof. Kumar, Study notes of Java Programming

A detailed guide on gui programming in java, covering various components such as button, label, textfield, textarea, checkbox, choice, list, and their respective syntaxes. It includes examples for each component, demonstrating their usage in awt. The document also covers event handling and event-driven programming.

Typology: Study notes

2023/2024

Available from 05/30/2024

tanu-10
tanu-10 🇮🇳

15 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
GUI Programming in Java |Madhu T.
GUI Programing with Java
Java Provides 2 Frameworks for building GUI-based applications. Those are
AWT-(Abstract Window Toolkit)
Swing
AWT-(Abstract Window Toolkit):
AWT (Abstract Window Toolkit) is an API to develop GUI or window-based
applications in java.
The java.awt package provides classes for AWT API such as TextField, Label,
TextArea, Checkbox, Choice, List etc.
AWT components are platform-dependent i.e. components are displayed
according to the view of operating system.
AWT Hierarchy
Component:
Component is an abstract class that contains various classes such as Button,
Label,Checkbox,TextField, Menu and etc.
Container:
The Container is a component in AWT that can contain another components like
buttons, textfields, labels etc. The Container class extends Frame and Panel.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download GUI Programming in Java: A Comprehensive Guide - Prof. Kumar and more Study notes Java Programming in PDF only on Docsity!

GUI Programing with Java

Java Provides 2 Frameworks for building GUI-based applications. Those are

 AWT-(Abstract Window Toolkit)  Swing

AWT-(Abstract Window Toolkit):

  • AWT (Abstract Window Toolkit) is an API to develop GUI or window-based applications in java.
  • The java.awt package provides classes for AWT API such as TextField, Label, TextArea, Checkbox, Choice, List etc.
  • AWT components are platform-dependent i.e. components are displayed according to the view of operating system.

AWT Hierarchy

  • Component:

Component is an abstract class that contains various classes such as Button, Label,Checkbox,TextField, Menu and etc.

  • Container:

The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. The Container class extends Frame and Panel.

  • Window: The window is the container that have no borders and menu bars. You must use frame for creating a window.
  • Frame: The Frame is the container that contain title bar and can have menu bars. It can have other components like button, textfield etc.
  • Panel: The Panel is the container that doesn't contain title bar and menu bars. It can have other components like button, textfield etc.

Commonly used Methods of Component class

Method Description

add(Component c) inserts a component on this component.

setSize(int width,int height) sets the size (width and height) of the component.

setLayout(LayoutManager m) defines the layout manager for the component.

setVisible(boolean status) changes the visibility of the component, by default false.

To create simple awt example, you need a frame. There are two ways to create a frame in AWT.

  • By extending Frame class (inheritance) Ex: class Example extends Frame { …….. }
  • By creating the object of Frame class (association) Ex: class Example { Frame obj=new Frame(); …….. }

Label:

The Label class is a component for placing text in a container. It is used to display a single line of read only text. The text can be changed by an application but a user cannot edit it directly.

Syntax: Label l1=new Label(“Text”); (or) Label l1,l2; l1=new Label(“Text”);  TextField: The TextField class is a text component that allows the editing of a single line text. Syntax: TextField t1=new TextField(“Text”); (or) TextField t1,t2; t1=new TextField(“Text”);

  • TextArea : The TextArea class is a multi line region that displays text. It allows the editing of multiple line text. Syntax: TextArea t1=new TextArea(“Text”); (or) TextArea t1,t2; t1=new TextArea(“Text”);
  • Checkbox : The Checkbox class is used to create a checkbox. It is used to turn an option on (true) or off (false). Clicking on a Checkbox changes its state from "on" to "off" or from "off" to "on". Syntax: Checkbox c1=new Checkbox(“Text”); (or) Checkbox c1,c2; c1=new Checkbox(“Text”);
  • Choice : The Choice class is used to show popup menu of choices. Choice selected by user is shown on the top of a menu.

Syntax: Choice c= new Choice(); c.add("Item 1"); c.add("Item 2"); c.add("Item 3");

  • List : The List class represents a list of text items. By the help of list, user can choose either one item or multiple items.

Syntax: List ls= new List(Size); ls.add("Item 1"); ls.add("Item 2"); ls.add("Item 3");

 AWT does allow the user to close window directly…

 We need code to close window //Code for Close window addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we) { System.exit(0); } });

Note: We need to import one package “java.awt.event.”.*

Example: An example for Label Component in AWT. LabelExample.java import java.awt.; import java.awt.event.; class LabelExample { public static void main(String args[]) { Frame f= new Frame(); f.setSize(400,400); f.setLayout(null); f.setVisible(true); f.setTitle("Label Example"); Label l1,l2; l1=new Label("User Name :"); l1.setBounds(50,100, 100,30); l2=new Label("Password :"); l2.setBounds(50,150, 100,30); f.add(l1); f.add(l2);

//Code for Close window f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we) { System.exit(0); } }); } } Output: Javac LabelExample.java Java LabelExample

Example: An example for TextField Component in AWT. TextFieldExample.java import java.awt.; import java.awt.event.; class TextFieldExample { public static void main(String args[]){ Frame f= new Frame(); f.setSize(400,400); f.setLayout(null); f.setVisible(true); f.setTitle("TextField Example"); TextField t1,t2; t1=new TextField(""); t1.setBounds(50,100, 200,30); t2=new TextField(""); t2.setBounds(50,150, 200,30); f.add(t1); f.add(t2); //Close window f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we) { System.exit(0); } }); } } Output: Javac TextFiledExample.java Java TextFiledExample

Example: An example for Checkbox Component in AWT. CheckboxExample.java import java.awt.; import java.awt.event.; public class CheckboxExample { public static void main(String args[]) { Frame f= new Frame("Checkbox Example"); f.setSize(400,400); f.setLayout(null); f.setVisible(true); f.setTitle("Checkbox Example"); Checkbox chb1 = new Checkbox("DS C++"); chb1.setBounds(100,100, 100,50); Checkbox chb2 = new Checkbox("Java", true); chb2.setBounds(100,150, 50,50); f.add(chb1); f.add(chb2); //Close window f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we) { System.exit(0); } }); } } Output: Javac CheckboxExample.java Java CheckboxExample

Example: An example for Choice Component in AWT. ChoiceExample.java import java.awt.; import java.awt.event.; public class ChoiceExample { public static void main(String args[]) { Frame f= new Frame(); f.setSize(400,400); f.setLayout(null); f.setVisible(true); f.setTitle("ChoiceExample"); Label l=new Label("Country :"); l.setBounds(50,100,75,75); Choice c=new Choice(); c.setBounds(120,125,75,75); c.add("India"); c.add("Japan"); c.add("Austraila"); c.add("U.S.A"); c.add("U.K"); f.add(c); f.add(l); //code for close window f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we) { System.exit(0); } }); } } Output: Javac ChoiceExample.java Java ChoiceExample

Example: An example program for Login page in AWT. LoginExample.java import java.awt.; import java.awt.event.; class LoginExample { public static void main(String args[]) { Frame f= new Frame ("Login Page"); f.setSize(400,400); f.setLayout(null); f.setVisible(true); Label l1,l2; TextField t1,t2; Checkbox cb; Button b; l1=new Label("User Name :"); l1.setBounds(50,60,100,30); t1=new TextField(""); t1.setBounds(150,60, 200,30); l2=new Label("Password :"); l2.setBounds(50,120,100,30); t2=new TextField(""); t2.setBounds(150,120, 200,30); cb=new Checkbox("Save Password"); cb.setBounds(50,150,200,30); b=new Button("Login"); b.setBounds(150,180,100,30); f.add(l1); f.add(l2); f.add(t1); f.add(t2); f.add(cb); f.add(b); } } Output: Javac LoginExample.java Java LoginExample

Example: An example program for Registration page in AWT. RegistrationExample.java import java.awt.; import java.awt.event.; class RegistrationExample { public static void main(String args[]) { Frame f= new Frame(); f.setSize(600,800); f.setLayout(null); f.setVisible(true); f.setTitle("Registration Page"); Label l1,l2,l3,l4,l5,l6; TextField t1,t2; Choice ch; List ls; Checkbox cb1,cb2,cb3; TextArea ta; Button b; l1=new Label("Name :"); l1.setBounds(50,60,100,30); t1=new TextField(""); t1.setBounds(150,60, 200,25); l2=new Label("Regd No :"); l2.setBounds(50,120,100,30); t2=new TextField(""); t2.setBounds(150,120, 200,25); l3=new Label("Year :"); l3.setBounds(50,180,100,30); ch=new Choice(); ch.setBounds(150,180,200,30); ch.add("I-YEAR"); ch.add("II-YEAR"); ch.add("III-YEAR"); ch.add("IV-YEAR"); l4=new Label("Branch :");

Event Handling

Event: Changing the state of an object (component) is known as an event. For example, click on button, dragging mouse etc.

Event describes the change in state of component. Events are generated as result of user interaction with the graphical user interface components. For example, clicking on a button, moving the mouse, entering a character through keyboard and selecting an item from list.

Def: Event Handling is the mechanism that controls the event and decides what should happen if an event occurs.

This mechanism have the code which is known as event handler that is executed when an event occurs. The java.awt.event package provides many event classes and Listener interfaces for event handling.

Steps to perform Event Handling Following steps are required to perform event handling:

  • Register the component with the Listener.

By using addActionListener(ActionListener a) Example: Button b=new Button(“Submit”); b.setBounds(100,50,80,30); b.addActionListener(this);

  • Provide or put event handling code.

By using actionPerformed(ActionEvent e) method of ActionListener Interface,we can perfom action what the user want. Example: Public void actionPerformed(ActionEvent e) { tf.setText(“welcome”); }

Simple Example: import java.awt.; import java.awt.event.; class EventExample extends Frame implements ActionListener{ TextField tf; EventExample(){ tf=new TextField(); //create components tf.setBounds(60,50,170,20); Button b=new Button("click me"); b.setBounds(100,120,80,30); b.addActionListener(this); //register listener &passing current instance add(b);add(tf); //add components and set size, layout and visibility setSize(300,300); setLayout(null); setVisible(true); } public void actionPerformed(ActionEvent e) { tf.setText("Welcome"); } public static void main(String args[]){ new EventExample(); } } Output: javac EventExample.java java EventExample