CBOP3203 What Is an Event? Events Objects that describe what - - PowerPoint PPT Presentation

cbop3203 what is an event
SMART_READER_LITE
LIVE PREVIEW

CBOP3203 What Is an Event? Events Objects that describe what - - PowerPoint PPT Presentation

CBOP3203 What Is an Event? Events Objects that describe what happened Event Sources The generator of an event Event Handlers A method that receives an event object, deciphers it, and processes the users interaction.


slide-1
SLIDE 1

CBOP3203

slide-2
SLIDE 2

 What Is an Event?

  • Events – Objects that describe what happened
  • Event Sources – The generator of an event
  • Event Handlers – A method that receives an event
  • bject, deciphers it, and processes the user’s

interaction.

The user clicks

  • n the button

actionPerformed(ActionEvent e) { }

Some event handler

ActionEvent

slide-3
SLIDE 3

 Event Delegation Model describes the steps that

need to be followed by a programmer in order to insert codes for event handling in a program.

 With

this model, events are sent to the component from which the event originated, but it is up to each component to propagate the event to one or more registered classes called

  • listeners. Listeners contain event handlers that

receive and process the event. In this way, the event handler can be in an object separate from the component. Listeners are classes that implement the EventListener interface.

slide-4
SLIDE 4

 An event can be sent to many event handlers.  Event

handlers register with components when they are interested in events generated by that component.

slide-5
SLIDE 5

 Client objects (handlers) register with a GUI

component that they want to observe

 GUI components trigger only the handlers for

the type of event that has occurred

 Most components can trigger more than one

type of event.

 The delegation model distributes the work

among multiple classes.

slide-6
SLIDE 6
slide-7
SLIDE 7
slide-8
SLIDE 8
slide-9
SLIDE 9
slide-10
SLIDE 10

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class btnListener extends JApplet implements ActionListener { public void init(){ Container conpane=getContentPane(); conpane.setLayout(new FlowLayout()); JButton btnTest=new JButton("Press Me"); btnTest.addActionListener(this); conpane.add(btnTest); } public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog( null, "Why did you press me?" ); } }

Make sure the java.awt.event.* package is imported Use the listener interface together with the implements statements

slide-11
SLIDE 11

import javax.swing.*; import java.awt.*; public class colorApplet extends JApplet { public void init( ){ Container conpane=getContentPane( ); conpane.add(new Pattern()); } }

Save this class as colorApplet.java

slide-12
SLIDE 12

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Pattern extends JPanel implements ActionListener { private JButton btnRed,btnGreen,btnBlue; public Pattern() { btnRed = new JButton("red"); btnGreen = new JButton("green"); btnBlue = new JButton("blue"); btnRed.addActionListener(this); btnGreen.addActionListener(this); btnBlue.addActionListener(this); add(btnRed); add(btnGreen); add(btnBlue); } public void actionPerformed(ActionEvent e) { Object source = e.getSource( ); Color clr = Color.white; if (source==btnRed) clr = Color.red; else if (source==btnGreen) clr = Color.green; else if (source==btnBlue) clr = Color.blue; setBackground(clr); repaint(); } }

Save this class as Pattern.java implements ActionListener When the Button object is clicked

  • n with the mouse, an ActionEvent

is sent. The ActionEvent is received through the actionPerformed() method of any ActionListener that is registered on the button through its addActionListener() method

slide-13
SLIDE 13

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class calculateApplet extends JApplet implements ActionListener { JTextField txt1,txt2,txt3; JLabel lbl1,lbl2,lbl3; public void init( ) { Container conpane = getContentPane( ); conpane.setLayout(new GridLayout(3,2)); lbl1 = new JLabel("First Number"); lbl2 = new JLabel("Second Number"); lbl3 = new JLabel("Sum of two integers"); txt1 = new JTextField("0",10); txt2 = new JTextField("0",10); txt3 = new JTextField("0",10); txt1.addActionListener(this); txt2.addActionListener(this); txt3.addActionListener(this); conpane.add(lbl1); conpane.add(txt1); conpane.add(lbl2); conpane.add(txt2); conpane.add(lbl3); conpane.add(txt3); } public void actionPerformed(ActionEvent e) { int num1 = Integer.parseInt(txt1.getText( )); int num2 = Integer.parseInt(txt2.getText( )); int num3 = num1 + num2; txt3.setText(String.valueOf(num3)); } }

slide-14
SLIDE 14

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Alphabet extends JApplet implements ItemListener { private JTextField txtInput; private JCheckBox chkBold,chkItalic; public void init( ){ Container conpane = getContentPane( ); conpane.setLayout(new FlowLayout( )); txtInput = new JTextField("",20); chkBold = new JCheckBox("Bold",false); chkItalic = new JCheckBox("Italic",false); chkBold.addItemListener(this); chkItalic.addItemListener(this); conpane.add(txtInput); conpane.add(chkBold); conpane.add(chkItalic); } public void itemStateChanged(ItemEvent e){ int t=0,c=0; Font font; if (chkBold.isSelected( )) t = Font.BOLD; if (chkItalic.isSelected( )) { c = Font.ITALIC; } font = new Font("Serif",t + c, 14); txtInput.setFont(font); } }

There are a few components that use the ItemListener of the event

  • listener. Among the components

used are the JCheckBox and JList. The ItemListener performs the itemStateChanged method to implement action to the events. While addItemListener is used to register the component.

slide-15
SLIDE 15

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RGBColor extends JApplet implements AdjustmentListener { private JLabel lblRed; private JLabel lblGreen; private JLabel lblBlue; private JScrollBar barRed; private JScrollBar barGreen; private JScrollBar barBlue; private JPanel panelColor; public void init() { Container conpane = getContentPane(); JPanel p = new JPanel( ); p.setLayout(new GridLayout(3, 2)); lblRed = new JLabel("Red 0"); p.add(lblRed); barRed=new JScrollBar(Adjustable.HORIZONTAL, 0,0, 0, 255); p.add(barRed); barRed.setBlockIncrement(16); barRed.addAdjustmentListener(this);

The java file continues on next page

slide-16
SLIDE 16

lblGreen = new JLabel("Green 0"); p.add(lblGreen); barGreen = new JScrollBar(Adjustable.HORIZONTAL , 0, 0, 0,255); p.add(barGreen); barGreen.setBlockIncrement(16); barGreen.addAdjustmentListener(this); lblBlue = new JLabel("Blue 0"); p.add(lblBlue); barBlue= new JScrollBar(Adjustable.HORIZONTAL, 0, 0, 0,255); p.add(barBlue); barBlue.setBlockIncrement(16); barBlue.addAdjustmentListener(this); conpane.add(p, "South"); panelColor = new JPanel(); panelColor.setBackground(new Color(0, 0, 0)); conpane.add(panelColor, "Center"); } public void adjustmentValueChanged(AdjustmentEvent evt){ lblRed.setText("Red " + barRed.getValue()); lblGreen.setText("Green " + barGreen.getValue()); lblBlue.setText("Blue " + barBlue.getValue()); panelColor.setBackground(new Color(barRed.getValue(), barGreen.getValue( ), barBlue.getValue())); panelColor.repaint(); } }

slide-17
SLIDE 17

 Q1. Write a temperature conversion applet

that converts from Fahrenheit to Celsius. The Fahrenheit temperature should be entered from the keyboard (via a JTextField). A JLabel should be used to display the converted temperature. Use the following formula for the conversion:

slide-18
SLIDE 18

 Q2.

Enhance the temperature conversion applet

  • f

Q1 by adding the Kelvin temperature scale. The applet should also allow the user to make conversions between any two scales. Use the following formula for the conversion between Kelvin and Celsius (in addition to the formula in Q1):