CSE 114 – Computer Science I Event Programming

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
F27SB2 Programming Languages
TCU CoSc Programming with Java Handling Events.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Events and the AWT The objectives of this chapter are: To understand the principles of the Java 1.1 event model To understand how the event model is used.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Event Handling Events and Listeners Timers and Animation.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 16 Java Fundamentals Java2 Graphical User Interfaces.
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Graphical User Interface Components: Part 1 Chapter 11.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
UID – Event Handling and Listeners Boriana Koleva
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
Object Oriented Programming.  Interface  Event Handling.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Creating User Interfaces Event-Driven Programming.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
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.
Event Handling and Listeners in SWING The practice of event handling.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
GUI Programming in Java Hao Jiang Boston College April, 2009.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
GUIs and Events Rick Mercer.
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Chapter 14 Event-Driven Programming
Welcome To java
CompSci 230 S Programming Techniques
Object-Orientated Analysis, Design and Programming
Chapter 12 Event-Driven Programming
Aum Amriteshwaryai Namah
Handling User Events with Swing
Web Design & Development Lecture 11
A First Look at GUI Applications
Advanced User Interfaces
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
GUI Programming III: Events
Event-driven programming for GUI
Web Design & Development Lecture 12
Making Java GUIs Functional
Presentation transcript:

CSE 114 – Computer Science I Event Programming Gros Morne N.P., Newfoundland

Event Programming For the User to interact with a GUI, the underlying Operating System must support event handling Operating Systems constantly monitor events Ex: keystrokes, mouse clicks, etc… Operating Systems sort out these events and report them to the appropriate programs

AWT Event Handling Event Sources and Event Listeners For Events that AWT knows about, you control how events are transmitted from event sources to event listeners An event source (e.g., JButton) object registers listener objects: button.addActionListener(panel) When an event occurs, the event source object sends out event objects to all registered listener objects A listener object implements a listener interface Listener objects use the information in the event object to determine their reaction to the event different methods inside a listener object can react differently to different types of interactions

Kinds of Source Objects Swing Components JButton, JCheckbox, JPanel, etc… Each may produce different kinds of events

Kinds of Event and Listener Objects Different event sources can produce different kinds of events ActionEvent (sent by JButton and other components) MouseEvent (sent by JPanel and other components) Different listener objects implement different required methods from interfaces ActionListener - (actionPerformed) MouseListener - (mouseClicked, mouseEntered, mouseExited, mousePressed, mouseReleased)

ActionListener Interface Single method in the interface public void actionPerformed(ActionEvent e) The ActionEvent class has methods to get information about the event public Object getSource() returns the GUI object that generated the ActionEvent. you may then react differently to different components having generated such an event. public String getActionCommand() returns the action command of the GUI object that generated the ActionEvent for each GUI component, you may setActionCommand for buttons, the action command is the text on the button

Selecting Event Listeners Designate a class to handle ActionEvents for GUI components. Such a class: implements the ActionListener interface Defines the actionPerformed method to react to interactions with various components

Event Handling Summary Necessary code: Specify a class to implement a listener interface implements ActionListener Code that implements the methods of the listener interface public void actionPerformed(ActionEvent e) {…} Code that registers a listener object on an event source cutButton.addActionListener(this)

JButton Class Hierarchy Object Component Container JComponent Window Frame JPanel AbstractButton JFrame JButton

JButton Class An implementation of a push button Constructors – button with an icon JButton cutButton = new JButton( new ImageIcon("cut.gif")); button with text JButton cutButton = new JButton("Cut"); button with an icon and text Some methods: addActionListener, doClick

Using a JFrame class as a listener public class ButtonFrame extends JFrame implements ActionListener { private JPanel buttonPanel = new JPanel(); private JButton cutButton = new JButton("Cut"); private JButton copyButton = new JButton("Copy"); private JButton pasteButton = new JButton("Paste"); private JTextArea textArea = new JTextArea(); private JScrollPane jsp = new JScrollPane(textArea); public ButtonFrame() super("ButtonFrame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 200); setLocation(0,0); buttonPanel.add(cutButton); buttonPanel.add(copyButton); buttonPanel.add(pasteButton);

cutButton.addActionListener(this); copyButton.addActionListener(this); pasteButton.addActionListener(this); Container contentPane = getContentPane(); contentPane.add(buttonPanel, BorderLayout.NORTH); contentPane.add(jsp, BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if (command.equals("Cut")) textArea.cut(); else if (command.equals("Copy")) textArea.copy(); else if (command.equals("Paste")) textArea.paste(); public static void main(String args[]) { ButtonFrame frame = new ButtonFrame(); frame.setVisible(true);

Executing the ButtonFrame program

AWT Events Passed to Listeners Semantic events ActionEvent – button click, menu selection, etc. AdjustmentEvent – scrollbar adjustment ItemEvent – selection from a set of checkboxes TextEvent – contents of a text area changed Low-level events (inherit from ComponentEvent) ComponentEvent – component resized, hidden, etc. ContainerEvent – component added or removed FocusEvent – a component gets focus or loses focus KeyEvent – key was pressed or released MouseEvent – mouse button pressed, released, etc. WindowEvent – window activated, deactivated, etc.

AWT Listeners to handle Events Listeners for Semantic events ActionListener AdjustmentListener ItemListener TextListener Listeners for low-level events ComponentListener ContainerListener FocusListener KeyListener MouseListener MouseMotionListener WindowListener

Commonly Used Swing Components JTextField JTextArea JOptionPane

JTextField Used to collect and display a single line of text Label using a JLabel Constructor JTextField(String text, int columns) Some methods addActionListener(ActionListener a) getSelectedText() getText() setEditable(boolean b) setFont(Font f) setHorizontalAlignment(int alignment) setText(String t) Keyboard “Enter” fires ActionEvents

JTextArea Used to collect and display multiple lines of text Rows Columns Used to collect and display multiple lines of text Constructor: textArea = new JTextArea(8, 40); JTextAreas do not have scroll bars, so use a JScrollPane: JTextArea textArea = new JTextArea(8, 40); contentPane.add(new JScrollPane(textArea), BorderLayout.CENTER); Other useful methods: append(String str) getText() insert(String str, int pos) setEditable(boolean b) setFont(Font f) setHorizontalAlignment setLineWrap(boolean b) setText(String t) setWordWrap(boolean b)

Option Dialogs Swing’s set of ready-made simple dialogs. has an icon, a message, and one or more option buttons. The JOptionPane class has four static methods to show these dialogs: showMessageDialog¾ Show a message and wait for the user to click Ok. showConfirmDialog¾ Show a message and get a confirmation (like Ok / Cancel). showOptionDialog¾ Show a message and get a user option from a set of options. showInputDialog¾ Show a message and get one line of user input.

Dialog Components An icon (depends on message type, or supply your own icon) Message type can be ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, PLAIN_MESSAGE A message, which can be any of the following: String: Draw the string Icon: Show the icon Component: Show the component Object[]: Show all objects stacked on each other One or more option buttons

Input dialog: Has an additional component for user input Further Options Input dialog: Has an additional component for user input text field combo box Confirm dialog: Uses four option types DEFAULT_OPTION YES_NO_OPTION YES_NO_CANCEL_OPTION OK_CANCEL_OPTION Each of the dialogs have further options available

Example int selection = JOptionPane.showConfirmDialog(parent, “Message”, “Title”, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (selection == JOptionPane.OK_OPTION) { …