CBOP3203 What Is an Event? Events Objects that describe what - - PowerPoint PPT Presentation
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.
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
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.
An event can be sent to many event handlers. Event
handlers register with components when they are interested in events generated by that component.
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.
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
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
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
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)); } }
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.
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
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(); } }
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:
Q2.
Enhance the temperature conversion applet
- f