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 user s interaction. The user clicks on the button ActionEvent Some event handler actionperformed(actionevent e) {
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); Make sure the java.awt.event.* package is imported Use the listener interface together with the implements statements public void actionperformed(actionevent e) { JOptionPane.showMessageDialog( null, "Why did you press me?" );
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 on 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 of 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):