EVENT HANDLING. (lecture 12)

EVENT HANDLING (lecture ‐12) Example GUI import java.awt.*; import javax.swing.*; import javax.swing.border.*; public class TestSwingCommonFeatur...
Author: Rosa McKinney
4 downloads 1 Views 287KB Size
EVENT HANDLING (lecture ‐12)

Example GUI

import java.awt.*; import javax.swing.*; import javax.swing.border.*;

public class TestSwingCommonFeatures extends JFrame { public TestSwingCommonFeatures() { // C Create a panell to group three h b buttons

JPanel Jbutton JButton JButton

p1 = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 2)); jbtLeft = new JButton("Left"); jbtCenter = new JButton("Center"); jbtRight = new JButton("Right");

jbtLeft.setBackground(Color.WHITE); jbtCenter.setForeground(Color.GREEN); jbtRi ht tT lTi T t("Thi iis th jbtRight.setToolTipText("This the Ri Right ht b button"); tt ") p1.add(jbtLeft); p1.add(jbtCenter); p1.add(jbtRight); p1.setBorder(new TitledBorder("Three Buttons"));

// Create a font and a line border

Font largeFont = new Font("TimesRoman", Font.BOLD, 20); Border lineBorder = new LineBorder(Color.BLACK, 2); // Create a panel to group two labels

JPanel JLabel JLabel

p2 = new JPanel(new GridLayout(1, 2, 5, 5)); jlblRed = new JLabel("Red"); jlblOrange = new JLabel("Orange");

jlblRed.setForeground(Color.RED); jlblOrange.setForeground(Color.ORANGE); jlblRed.setFont(largeFont); jlblOrange.setFont(largeFont); jlblRed.setBorder(lineBorder); jlblOrange.setBorder(lineBorder); p2.add(jlblRed); p2.add(jlblOrange); p2.setBorder(new TitledBorder("Two Labels")); // Add two panels to the frame

setLayout(new GridLayout(2, 1, 5, 5)); add(p1);

add(p2);

} // rest of the code }

Event driven Programming

Event driven Programming • The signals that a program receives from the OS as a result of your actions are called events • A window based program is called event driven program • Unlike the old rigid old sequential programs, – it puts user in charge, user control the sequence of program – Application waits for the user action – This approach pp is called event driven p programming g g

Event Delegation Model • The Delegation Event Model – Model used by Java to handle user interaction with GUI components – Describes how your program can respond to user interaction

• Three Th iimportant t t players l – Event Source – Event Listener/Handler – Event Object

Event Delegation Model • Event Source – GUI component that generates the event – Example: button

• Event Listener/Handler – Receives and handles events – Contains business logic – Example: p displaying p y g information useful to the user,, computing p g a value

• Event Object – Created when an event occurs (i.e., user interacts with a GUI component) t) – Contains all necessary information – Represented by an Event class

Event Basics • All Events are objects of Event Classes. • All Event Classes derive from EventObject. • When an Event occurs, occurs Java sends a message to all registered Event Listeners from the Event source

Event Classes • The EventObject class – Found in the java.util package

• The AWTEvent class – An immediate subclass of EventObject – Defined in java.awt package – Root of all AWT‐based events

3 Steps for event Handler • Either implements p a listener interface or extends a class that implements a listener interface • Register your listener • someComponent.addActionListener(instanceOfMyClass); • Implement user action public void actionPerformed(ActionEvent p ( e)) { ...//code that reacts to the action... }

Example public class TryWindow extends JFrame implements ActionListener{ static JTextField tf=new JTextField(" no "); static int count=0;; public void actionPerformed(ActionEvent e){ count++; tf.setText("hiiii f ("hiiii "+count); " ) } Public static void main(String arg[]){ JButton b = new JButton( JButton("one"); one ); TryWindow frame=new TryWindow(); b.addActionListener(frame); panel1.setLayout(new FlowLayout()); panel1.add(b); panel1.add(tf); //other code for frame

}

Java Event Types, Listeners & Listener Methods Event Class

Listener Interface

Listener Methods

ActionEvent

ActionListener

actionPerformed()

AdjustmentEvent

AdjustmentListener

adjustmentValueChanged()

ComponentEvent

ComponentListener

componentHidden() componentMoved() componentResized() componentShown()

ContainerEvent

ContainerListener

componentAdded() componentRemoved()

FocusEvent

FocusListener

focusGained() focusLost()

Java Event Types, Listeners & Listener Methods Event Class

Listener Interface

Listener Methods

ItemEvent

ItemListener

itemStateChanged()

KeyEvent

KeyListener

keyPressed() keyReleased() keyTyped()

MouseEvent

MouseListener

mouseClicked() mouseEntered() mouseExited() mousePressed() mouseReleased() mo seDragged() mouseDragged()

MouseMotionListener

mouseMoved()

Java Event Types, Listeners & Listener Methods Event Class

Listener Interface

Listener Methods

TextEvent

TextListener

textValueChanged()

WindowEvent

WindowListener

windowActivated() windowClosed() windowClosing() windowDeactivated() windowDeiconified() windowIconified() windowOpened() i d O d()

Mouse Listener Example import java.awt.*; ja a a t * import java.awt.event.*; p public class MouseDemo extends Frame implements p MouseListener,, MouseMotionListener { TextField tf;

public MouseDemo(String title){ super(title); tf = new TextField(60); addMouseListener(this); }

// Register event listener to the event source

public void launchFrame() { add(tf, BorderLayout.SOUTH); // Add components to the frame setSize(300,300); setVisible(true); }

Mouse Listener Example public void mouseClicked(MouseEvent me) { String msg = "Mouse clicked."; tf.setText(msg); } public void mouseEntered(MouseEvent me) { String msg = "Mouse entered component."; tf.setText(msg); } public void mouseExited(MouseEvent me) { String msg = "Mouse exited component."; tf.setText(msg); } public void mousePressed(MouseEvent me) { String msg = "Mouse pressed."; tf.setText(msg); }

Mouse Listener Example public void mouseReleased(MouseEvent me) { String msg = "Mouse released."; tf.setText(msg); } public void mouseDragged(MouseEvent me) { String msg = "Mouse dragged at " + me.getX() tf.setText(msg);}

+ "," + me.getY();

public void mouseMoved(MouseEvent me) { String msg = "Mouse moved at " + me.getX() + "," + me.getY(); tf.setText(msg);} public static void main(String args[]) { MouseDemo med =new MouseDemo( MouseDemo("Mouse Mouse Events Demo Demo"); ); med.launchFrame(); }}

Window Listener Example import java.awt.*; import java.awt.event.*; class CloseFrame extends Frame implements WindowListener { CloseFrame(String title) { super(title); this.addWindowListener(this); } void launchFrame() { setSize(300,300); setVisible(true); }

Window Listener Example

//Implement methods of listener interface

public void windowActivated(WindowEvent e) { System.out.println(“Window is activated”); } public void windowClosed(WindowEvent p ( e)) {} public void windowClosing(WindowEvent e) { setVisible(false); System.exit(0); } public void windowDeactivated(WindowEvent e) {} public void windowDeiconified(WindowEvent e) { System.out.println(“Window is Deiconified”); } public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} public static void main(String args[ ]) { CloseFrame cf = new CloseFrame("Close Window Example"); cf.launchFrame(); }}