0% found this document useful (0 votes)
54 views

Java Event-And-Gui-Programming

Uploaded by

Sathya Ashok
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Java Event-And-Gui-Programming

Uploaded by

Sathya Ashok
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

lOMoARcPSD|31571928

Event and GUI Programming

Computer science and engineering (Davangere University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Sathya Ashok (sathyasaisid@gmail.com)
lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

AWT Program in Java


AWT stands for Abstract window toolkit is an Application programming interface (API)
for creating Graphical User Interface (GUI) in Java. It allows Java programmers to develop
window-based applications.

AWT provides various components like button, label, checkbox, etc. used as objects inside a
Java Program.

AWT components use the resources of the operating system, i.e., they are platform-
dependent, which means, component's view can be changed according to the view of the
operating system. The classes for AWT are provided by the Java.awt package for various
AWT components.

Event Handling in Java


An event can be defined as changing the state of an object or behavior by performing
actions. Actions can be a button click, cursor movement, keypress through keyboard or
page scrolling, etc. The java.awt.event package can be used to provide various event
classes.

Classification of Events

 Foreground Events
 Background Events

Types of Events

1. Foreground Events
Foreground events are the events that require user interaction to generate, i.e., foreground
events are generated due to interaction by the user on components in Graphic User Interface
(GUI). Interactions are nothing but clicking on a button, scrolling the scroll bar, cursor
moments, etc.

Prepared By:Hemavati,G.F.G Evening College Davangere 1

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

2. Background Events
Events that don’t require interactions of users to generate are known as background events.
Examples of these events are operating system failures/interrupts, operation completion,
etc.
Event Handling: It is a mechanism to control the events and to decide what should
happen after an event occur. To handle the events, Java follows the Delegation Event
model.

Delegation Event model

 It has Sources and Listeners.

Delegation Event Model

 Source: Events are generated from the source. There are various sources like buttons,
checkboxes, list, menu-item, choice, scrollbar, text components, windows, etc., to
generate events.
 Listeners: Listeners are used for handling the events generated from the source. Each
of these listeners represents interfaces that are responsible for handling events.
To perform Event Handling, we need to register the source with the listener.

Registering the Source With Listener

Different Classes provide different registration methods.


Syntax:
addTypeListener()
where Type represents the type of event.
Example 1: For KeyEvent we use addKeyListener() to register.
Example 2:that For ActionEvent we use addActionListener() to register.

Prepared By:Hemavati,G.F.G Evening College Davangere 2

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

Event Classes in Java

Event Class Listener Interface Description

An event that indicates that a component-


defined action occurred like a button
click or selecting an item from the menu-
ActionEvent ActionListener item list.

The adjustment event is emitted by an


AdjustmentEvent AdjustmentListener Adjustable object like Scrollbar.

An event that indicates that a component


moved, the size changed or changed its
ComponentEvent ComponentListener visibility.

When a component is added to a


container (or) removed from it, then this
ContainerEvent ContainerListener event is generated by a container object.

These are focus-related events, which


include focus, focusin, focusout, and
FocusEvent FocusListener blur.

An event that indicates whether an item


ItemEvent ItemListener was selected or not.

An event that occurs due to a sequence of


KeyEvent KeyListener keypresses on the keyboard.

The events that occur due to the user


MouseListener & interaction with the mouse (Pointing
MouseEvent MouseMotionListener Device).

An event that specifies that the mouse


MouseWheelEvent MouseWheelListener wheel was rotated in a component.

Prepared By:Hemavati,G.F.G Evening College Davangere 3

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

Event Class Listener Interface Description

An event that occurs when an object’s


TextEvent TextListener text changes.

An event which indicates whether a


WindowEvent WindowListener window has changed its status or not.

Note: As Interfaces contains abstract methods which need to implemented by the registered
class to handle events.
Different interfaces consists of different methods which are specified below.

Listener Interface Methods

ActionListener
 actionPerformed()

AdjustmentListener
 adjustmentValueChanged()

 componentResized()
 componentShown()
 componentMoved()
ComponentListener
 componentHidden()

 componentAdded()
ContainerListener
 componentRemoved()

 focusGained()
FocusListener
 focusLost()

ItemListener
 itemStateChanged()

 keyTyped()
 keyPressed()
KeyListener
 keyReleased()

 mousePressed()
 mouseClicked()
 mouseEntered()
 mouseExited()
MouseListener
 mouseReleased()

Prepared By:Hemavati,G.F.G Evening College Davangere 4

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

Listener Interface Methods

 mouseMoved()
MouseMotionListener
 mouseDragged()

MouseWheelListener
 mouseWheelMoved()

TextListener
 textChanged()

 windowActivated()
 windowDeactivated()
 windowOpened()
 windowClosed()
 windowClosing()
 windowIconified()
WindowListener
 windowDeiconified()

Any program that uses GUI (graphical user interface) such as Java application written for
windows, is event driven. Event describes the change in state of any object.

For Example : Pressing a button, Entering a character in Textbox, Clicking or Dragging a


mouse, etc.

Components of Event Handling

Event handling has three main components,

 Events : An event is a change in state of an object.

 Events Source : Event source is an object that generates an event.

 Listeners : A listener is an object that listens to the event. A listener gets notified
when an event occurs.

Prepared By:Hemavati,G.F.G Evening College Davangere 5

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

Event Classes Description Listener Interface

generated when button is pressed, menu-item is selected, list-item is


ActionEvent ActionListener
double clicked

generated when mouse is dragged, moved,clicked,pressed or released


MouseEvent MouseListener
and also when it enters or exit a component

KeyEvent generated when input is received from keyboard KeyListener

ItemEvent generated when check-box or list item is clicked ItemListener

TextEvent generated when value of textarea or textfield is changed TextListener

MouseWheelEvent generated when mouse wheel is moved MouseWheelListener

generated when window is activated, deactivated, deiconified,


WindowEvent WindowListener
iconified, opened or closed

ComponentEvent generated when component is hidden, moved, resized or set visible ComponentEventListener

ContainerEvent generated when component is added or removed from container ContainerListener

AdjustmentEvent generated when scroll bar is manipulated AdjustmentListener

FocusEvent generated when component gains or loses keyboard focus FocusListener

How Events are handled?

A source generates an Event and send it to one or more listeners registered with the source.
Once event is received by the listener, they process the event and then return. Events are
supported by a number of Java packages, like java.util, java.awt and java.awt.event.

Important Event Classes and Interface

Steps to handle events:

1. Implement appropriate interface in the class.

2. Register the component with the listener.

Prepared By:Hemavati,G.F.G Evening College Davangere 6

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

Example of Event Handling

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import java.applet.*;

import java.awt.event.*;

import java.awt.*;

public class Test extends Applet implements KeyListener

String msg="";

public void init()

addKeyListener(this);

public void keyPressed(KeyEvent k)

showStatus("KeyPressed");

public void keyReleased(KeyEvent k)

showStatus("KeyRealesed");

public void keyTyped(KeyEvent k)

msg = msg+k.getKeyChar();

repaint();

Prepared By:Hemavati,G.F.G Evening College Davangere 7

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

public void paint(Graphics g)

g.drawString(msg, 20, 40);

HTML code:

<applet code="Test" width=300, height=100>

</applet>

BASICS OF GUI
Java AWT is an API that contains large number of classes and methods to create and manage
graphical user interface ( GUI ) applications. The AWT was designed to provide a common
set of tools for GUI design that could work on a variety of platforms. The tools provided by
the AWT are implemented using each platform's native GUI toolkit, hence preserving the
look and feel of each platform. This is an advantage of using AWT. But the disadvantage of
such an approach is that GUI designed on one platform may look different when displayed on
another platform that means AWT component are platform dependent.

AWT is the foundation upon which Swing is made i.e Swing is a improved GUI API that
extends the AWT. But now a days AWT is merely used because most GUI Java programs are
implemented using Swing because of its rich implementation of GUI controls and light-
weighted nature.

Prepared By:Hemavati,G.F.G Evening College Davangere 8

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

Java AWT Hierarchy

The hierarchy of Java AWT classes are given below, all the classes are available
in java.awt package.

Prepared By:Hemavati,G.F.G Evening College Davangere 9

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

Component class

Component class is at the top of AWT hierarchy. It is an abstract class that encapsulates all
the attributes of visual component. A component object is responsible for remembering the
current foreground and background colors and the currently selected text font.

Container

Container is a component in AWT that contains another component like button, text field,
tables etc. Container is a subclass of component class. Container class keeps track of
components that are added to another component.

Panel

Panel class is a concrete subclass of Container. Panel does not contain title bar, menu bar or
border. It is container that is used for holding components.

Window class

Window class creates a top level window. Window does not have borders and menubar.

Frame

Frame is a subclass of Window and have resizing canvas. It is a container that contain
several different components like button, title bar, textfield, label etc. In Java, most of the
AWT applications are created using Frame window. Frame class has two different
constructors,

Frame() throws HeadlessException

Frame(String title) throws HeadlessException

Prepared By:Hemavati,G.F.G Evening College Davangere 10

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

Creating a Frame

There are two ways to create a Frame. They are,

1. By Instantiating Frame class

2. By extending Frame class

Creating Frame Window by Instantiating Frame class

import java.awt.*;

public class Testawt

Testawt()

Frame fm=new Frame(); //Creating a frame

Label lb = new Label("welcome to java graphics"); //Creating a label

fm.add(lb); //adding label to the frame

fm.setSize(300, 300); //setting frame size.

fm.setVisible(true); //set frame visibilty true

public static void main(String args[])

Testawt ta = new Testawt();

Prepared By:Hemavati,G.F.G Evening College Davangere 11

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

Creating Frame window by extending Frame class

package testawt;

import java.awt.*;

import java.awt.event.*;

public class Testawt extends Frame

public Testawt()

Button btn=new Button("Hello World");

add(btn); //adding a new Button.

setSize(400, 500); //setting size.

setTitle("StudyTonight"); //setting title.

setLayout(new FlowLayout()); //set default layout for frame.

setVisible(true); //set frame visibilty true.

Prepared By:Hemavati,G.F.G Evening College Davangere 12

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

public static void main (String[] args)

Testawt ta = new Testawt(); //creating a frame.

Points to Remember:

1. While creating a frame (either by instantiating or extending Frame class), Following


two attributes are must for visibility of the frame:

o setSize(int width, int height);

o setVisible(true);

2. When you create other components like Buttons, TextFields, etc. Then you need to
add it to the frame by using the method - add(Component's Object);

3. You can add the following method also for resizing the frame - setResizable(true);

AWT Button

In Java, AWT contains a Button Class. It is used for creating a labelled button which can
perform an action.

Prepared By:Hemavati,G.F.G Evening College Davangere 13

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

AWT Button Classs Declaration:

public class Button extends Component implements Accessible

Example:

Lets take an example to create a button and it to the frame by providing coordinates.

import java.awt.*;

public class ButtonDemo1

public static void main(String[] args)

Frame f1=new Frame("studytonight ==> Button Demo");

Button b1=new Button("Press Here");

b1.setBounds(80,200,80,50);

f1.add(b1);

f1.setSize(500,500);

f1.setLayout(null);

f1.setVisible(true);

Prepared By:Hemavati,G.F.G Evening College Davangere 14

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

AWT Label

In Java, AWT contains a Label Class. It is used for placing text in a container. Only Single
line text is allowed and the text can not be changed directly.

Label Declaration:

public class Label extends Component implements Accessible

Example:

In this example, we are creating two labels to display text to the frame.

import java.awt.*;

class LabelDemo1

public static void main(String args[])

Frame l_Frame= new Frame("studytonight ==> Label Demo");

Label lab1,lab2;

Prepared By:Hemavati,G.F.G Evening College Davangere 15

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

lab1=new Label("Welcome to studytonight.com");

lab1.setBounds(50,50,200,30);

lab2=new Label("This Tutorial is of Java");

lab2.setBounds(50,100,200,30);

l_Frame.add(lab1);

l_Frame.add(lab2);

l_Frame.setSize(500,500);

l_Frame.setLayout(null);

l_Frame.setVisible(true);

AWT TextField

In Java, AWT contains aTextField Class. It is used for displaying single line text.

TextField Declaration:

public class TextField extends TextComponent

Example:

Prepared By:Hemavati,G.F.G Evening College Davangere 16

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

We are creating two textfields to display single line text string. This text is editable in nature,
see the below example.

import java.awt.*;

class TextFieldDemo1{

public static void main(String args[]){

Frame TextF_f= new Frame("studytonight ==>TextField");

TextField text1,text2;

text1=new TextField("Welcome to studytonight");

text1.setBounds(60,100, 230,40);

text2=new TextField("This tutorial is of Java");

text2.setBounds(60,150, 230,40);

TextF_f.add(text1);

TextF_f.add(text2);

TextF_f.setSize(500,500);

TextF_f.setLayout(null);

TextF_f.setVisible(true);

Prepared By:Hemavati,G.F.G Evening College Davangere 17

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

AWT TextArea

In Java, AWT contains aTextArea Class. It is used for displaying multiple-line text.

TextArea Declaration:

public class TextArea extends TextComponent

Example:

In this example, we are creating a TextArea that is used to display multiple-line text string
and allows text editing as well.

import java.awt.*;

public class TextAreaDemo1

TextAreaDemo1()

Frame textArea_f= new Frame();

TextArea area=new TextArea("Welcome to studytonight.com");

area.setBounds(30,40, 200,200);

textArea_f.add(area);

textArea_f.setSize(300,300);

Prepared By:Hemavati,G.F.G Evening College Davangere 18

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

textArea_f.setLayout(null);

textArea_f.setVisible(true);

public static void main(String args[])

new TextAreaDemo1();

AWT Checkbox

In Java, AWT contains a Checkbox Class. It is used when we want to select only one option
i.e true or false. When the checkbox is checked then its state is "on" (true) else it is
"off"(false).

Checkbox Syntax

public class Checkbox extends Component implements ItemSelectable, Accessible

Example:

In this example, we are creating checkbox that are used to get user input. If checkbox is
checked it returns true else returns false.

Prepared By:Hemavati,G.F.G Evening College Davangere 19

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

import java.awt.*;

public class CheckboxDemo1

CheckboxDemo1(){

Frame checkB_f= new Frame("studytonight ==>Checkbox Example");

Checkbox ckbox1 = new Checkbox("Yes", true);

ckbox1.setBounds(100,100, 60,60);

Checkbox ckbox2 = new Checkbox("No");

ckbox2.setBounds(100,150, 60,60);

checkB_f.add(ckbox1);

checkB_f.add(ckbox2);

checkB_f.setSize(400,400);

checkB_f.setLayout(null);

checkB_f.setVisible(true);

public static void main(String args[])

new CheckboxDemo1();

Prepared By:Hemavati,G.F.G Evening College Davangere 20

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

AWT CheckboxGroup

In Java, AWT contains aCheckboxGroup Class. It is used to group a set of Checkbox. When
Checkboxes are grouped then only one box can be checked at a time.

CheckboxGroup Declaration:

public class CheckboxGroup extends Object implements Serializable

Example:

This example creates a checkboxgroup that is used to group multiple checkbox in a single
unit. It is helpful when we have to select single choice among the multiples.

import java.awt.*;

public class CheckboxGroupDemo

CheckboxGroupDemo(){

Frame ck_groupf= new Frame("studytonight ==>CheckboxGroup");

CheckboxGroupobj = new CheckboxGroup();

Checkbox ckBox1 = new Checkbox("Yes", obj, true);

ckBox1.setBounds(100,100, 50,50);

Checkbox ckBox2 = new Checkbox("No", obj, false);

ckBox2.setBounds(100,150, 50,50);

ck_groupf.add(ckBox1);

ck_groupf.add(ckBox2);

Prepared By:Hemavati,G.F.G Evening College Davangere 21

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

ck_groupf.setSize(400,400);

ck_groupf.setLayout(null);

ck_groupf.setVisible(true);

public static void main(String args[])

new CheckboxGroupDemo();

AWT Choice

In Java, AWT contains a Choice Class. It is used for creating a drop-down menu of choices.
When a user selects a particular item from the drop-down then it is shown on the top of the
menu.

Choice Declaration:

public class Choice extends Component implements ItemSelectable, Accessible

Example:

In this example, we are creating drop-down menu that is used to get user choice from
multiple choices.

import java.awt.*;

Prepared By:Hemavati,G.F.G Evening College Davangere 22

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

public class ChoiceDemo

ChoiceDemo()

Frame choice_f= new Frame();

Choice obj=new Choice();

obj.setBounds(80,80, 100,100);

obj.add("Red");

obj.add("Blue");

obj.add("Black");

obj.add("Pink");

obj.add("White");

obj.add("Green");

choice_f.add(obj);

choice_f.setSize(400,400);

choice_f.setLayout(null);

choice_f.setVisible(true);

public static void main(String args[])

new ChoiceDemo();

Prepared By:Hemavati,G.F.G Evening College Davangere 23

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

AWT List

In Java, AWT contains a List Class. It is used to represent a list of items together. One or
more than one item can be selected from the list.

List Declaration:

public class List extends Component implements ItemSelectable, Accessible

Example:

In this example, we are creating a list that is used to list out the items.

import java.awt.*;

public class ListDemo

ListDemo()

Frame list_f= new Frame();

List obj=new List(6);

obj.setBounds(80,80, 100,100);

obj.add("Red");

Prepared By:Hemavati,G.F.G Evening College Davangere 24

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

obj.add("Blue");

obj.add("Black");

obj.add("Pink");

obj.add("White");

obj.add("Green");

list_f.add(obj);

list_f.setSize(400,400);

list_f.setLayout(null);

list_f.setVisible(true);

public static void main(String args[])

new ListDemo();

Prepared By:Hemavati,G.F.G Evening College Davangere 25

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

MenuItem declaration

public class MenuItem extends MenuComponent implements Accessible

Menu declaration

public class Menu extends MenuItem implements MenuContainer, Accessible

Example:

In this example, we are creating a menu item that contains sub menu as well. We use
MenuItem and Menu class for creating menu.

import java.awt.*;

class MenuDemo1

MenuDemo1()

Frame menu_f= new Frame("studytonight ==> Menu and MenuItem Demo");

MenuBarmenu_bar=new MenuBar();

Prepared By:Hemavati,G.F.G Evening College Davangere 26

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

Menu menu11=new Menu("Menu");

Menu sub_menu1=new Menu("Sub Menu =>");

MenuItem a1=new MenuItem("Red");

MenuItem a2=new MenuItem("Light Red");

MenuItem a3=new MenuItem("Drak Red");

MenuItem b1=new MenuItem("Green");

MenuItem b2=new MenuItem("Light Green");

MenuItem b3=new MenuItem("Dark Green");

menu11.add(a1);

sub_menu1.add(a2);

sub_menu1.add(a3);

menu11.add(b1);

sub_menu1.add(b2);

sub_menu1.add(b3);

menu11.add(sub_menu1);

menu_bar.add(menu11);

menu_f.setMenuBar(menu_bar);

menu_f.setSize(400,400);

menu_f.setLayout(null);

menu_f.setVisible(true);

public static void main(String args[])

new MenuDemo1();

Prepared By:Hemavati,G.F.G Evening College Davangere 27

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

AWT PopupMenu

In Java, AWT contains a Popup Menu. It is a popup which is dynamic in nature.

PopupMenu declaration

public class PopupMenu extends Menu implements MenuContainer, Accessible

Example:

import java.awt.*;

import java.awt.event.*;

class PopupMenuDemo1

PopupMenuDemo1()

final Frame pop_menuf= new Frame("studytonight ==>PopupMenu Demo");

final PopupMenupop_menu = new PopupMenu("*Edit*");

MenuItempop_cut = new MenuItem("Cut");

pop_cut.setActionCommand("Cut");

MenuItempop_copy = new MenuItem("Copy");

pop_copy.setActionCommand("Copy");

MenuItempop_paste = new MenuItem("Paste");

Prepared By:Hemavati,G.F.G Evening College Davangere 28

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

pop_paste.setActionCommand("Paste");

pop_menu.add(pop_cut);

pop_menu.add(pop_copy);

pop_menu.add(pop_paste);

pop_menuf.addMouseListener(new MouseAdapter()

public void mouseClicked(MouseEvent a)

pop_menu.show(pop_menuf , a.getX(), a.getY());

});

pop_menuf.add(pop_menu);

pop_menuf.setSize(400,400);

pop_menuf.setLayout(null);

pop_menuf.setVisible(true);

public static void main(String args[])

new PopupMenuDemo1();

Prepared By:Hemavati,G.F.G Evening College Davangere 29

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)


lOMoARcPSD|31571928

DEPARTMENT OF CS EVENT AND GUI PROGRAMMING

Prepared By:Hemavati,G.F.G Evening College Davangere 30

Downloaded by Sathya Ashok (sathyasaisid@gmail.com)

You might also like