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

Ii Awt

Uploaded by

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

Ii Awt

Uploaded by

Umera Rawoot
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIT – II

AWT Components
Demo of AWT components
• AWT Components-1: Frame, Button, Label, TextField, TextArea
• AWT Components-2: Checkbox, CheckboxGroup, Choice (Radio
Button)
• AWT-Components-3: Canvas, Scrollbar, Menu, MenuItem
• AWT-Components-4: Panel, Dialog, PopupMenu, Toolkit
Canvas vs Frame vs Panel
• In java canvas is area used to draw something by java graphics. For ex.
drawing an image or rectangle.
• Frame is used as JFrame(swing), a top level container which can
contain canvas, panels, pane(DesktopPane, ScrollPane) etc..
• Panel or JPanel is a subcontainer used to contain textboxes, buttons,
canvas etc.
• Jframe can contain multiple panels, but panel can't contain JFrame.
Event and Listener (Java Event Handling)
• Changing the state of an object is known as an event. For example,
click on button, dragging mouse etc. The java.awt.event package
provides many event classes and Listener interfaces for event
handling.
Java Event classes and Listener interfaces
Event Classes Listener Interfaces
ActionEvent ActionListener
MouseEvent MouseListener and
MouseMotionListener
MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
Steps to perform Event Handling
• Following steps are required to perform event handling:

• Register the component with the Listener


• Registration Methods: For registering the component with the
Listener, many classes provide the registration methods. For example:
Registration Methods
• Button: public void addActionListener(ActionListener a){}
• MenuItem: public void addActionListener(ActionListener a){}
• TextField: public void addActionListener(ActionListener a){}
public void addTextListener(TextListener a){}
• TextArea: public void addTextListener(TextListener a){}
• Checkbox: public void addItemListener(ItemListener a){}
• Choice: public void addItemListener(ItemListener a){}
• List: public void addActionListener(ActionListener a){}
public void addItemListener(ItemListener a){}
Java Event Handling Code
• We can put the event handling code into one of the following places:
• Within class (Implementing ActionListener)
• Other class (Outer class)
• Anonymous class
Java event handling by implementing ActionListener

• AEvent.java
Java event handling by outer class

• AEvent2.java
Java event handling by anonymous class

• AEvent3.java

You might also like