UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 15 Java Fundamentals Java2 Graphical User Interfaces.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
F27SB2 Programming Languages
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
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.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
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.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
Java Programming Chapter 10 Graphical User Interfaces.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Graphical User Interface Components: Part 1 Chapter 11.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
CS102 – GUI AWT & Swing Components & Containers, Layout Managers, Events & Listeners MVC design pattern. David Davenport.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
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.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Object Oriented Programming.  Interface  Event Handling.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
Events (Chapter 11) Java Certification Study Group January 25, 1999 Mark Roth.
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.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
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.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
© Copyright by Pearson Education, Inc. All Rights Reserved. Appendix I GUI Components and Event Handling Android How to Program, 3/e.
A Quick Java Swing Tutorial
GUI Programming using Java - Event Handling
CSC 205 Programming II Lecture 5 AWT - I.
Welcome To java
CHAPTER Reacting to the user.
Aum Amriteshwaryai Namah
Appendix I GUI Components and Event Handling
A Quick Java Swing Tutorial
CSE 114 – Computer Science I Event Programming
GUI Programming III: Events
Introduction to Event Handling
A Quick Java Swing Tutorial
Presentation transcript:

UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 15 Java Fundamentals Java2 Graphical User Interfaces (GUIs) Fri. 10/13/00

Homework Status 1 Fri, 9/8 Fri, 9/15 Part 1 Mon, 9/18 Part 2 Mon, 9/18 Part 2 2Fri, 9/15 Fri, 9/22 Part 1 & Part 2 3Fri, 9/22 Fri, 9/29Part 1 & Part 2 4Fri, 10/6Fri, 10/13Part 1 & Part 2 5Fri, 10/13Fri, 10/20Part 1 & Part 2 HW# Assigned Due Content

Overview ä Events ä Widgets (GUI components) ä Containers ä Labels ä Buttons ä Text fields ä Select lists

GUIs Are Important ä GUIs need to provide a skillful blend of: ä User productivity - tasks are accomplished quickly ä User safety - mistakes are minimized ä Intuitiveness - training/learning effort is minimized

GUIs Are Hard to Build ä Event loop processing is a new, somewhat complicated paradigm ä Testing is a challenge (hard to exercise all user options) ä Layout requires task-based human factors considerations

Events ä The user interacts with your program by initiating “events” ä Mouse move ä Mouse button (up/down) ä Keyboard key (up/down)

Event-driven Programming ä Set up the user environment ä Create and initialize widgets ä Register event handlers with specific widgets ä Enter the “event loop” ä Wait for an event (listen) ä Handle the event (respond)

Handling Events ä Get the user input (if appropriate) ä Perform appropriate processing based on user action ä Refresh the user interface

Event-driven Programming in Java ä The event loop is hidden from you (you show() the top-level window) ä Java puts event information into an instance of a class derived from AWTEvent (depending on event type) ä Event instances are “sent” to widgets automatically ä Widgets pass off events to the appropriate handler(s)

Java Uses Event Delegation ä Widgets are represented by instances of various Java library classes ä Widgets maintain a list of event handlers (an EventListenerList) ä Event handlers are EventListeners (i.e., implement an EventListener interface) and are keyed to specific event types (they “listen” for certain types of events)

Event Delegation (concluded) ä EventListeners are programmer-selected classes that are distinct from the widget ä EventListeners are interfaces ä Widgets attach implementations of these listener interfaces ä Such implementations are often done via an anonymous inner class ä These implementations “handle” the event

Example of an Event Listener public class MyFrame extends JFrame {... // inside of a constructor, for example jTextField1 = new JTextField(); jTextField1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) { jTextField1ActionPerformed(evt); jTextField1ActionPerformed(evt); } } ); // end of addActionListener ); // end of addActionListenergetContentPane().add(jTextField1);...}

Example of an Event Listener public class MyFrame extends JFrame {... private void jTextField1ActionPerformed (ActionEvent evt) { String inputString = jTextField1.getText(); String inputString = jTextField1.getText(); myColor = myStringToColor(inputString); myColor = myStringToColor(inputString); updateWidgets(); updateWidgets(); Container c = getContentPane(); Container c = getContentPane(); c.repaint(); c.repaint();}...}

Java Events ä Java aggregates low-level user events to make life easier for the developer ä Depending on the “focus” (“active” widget), e.g.: ä Button selected ä Text entered ä List item selected ä Menu item selected (we won’t discuss Java menus in this course) ä Item dragged (we won’t discuss Java drag and drop in this course)

Types of Java Events ä ActionEvent ä AdjustmentEvent ä ItemEvent ä ComponentEvent ä ContainerEvent ä FocusEvent ä PaintEvent ä WindowEvent ä Input Event ä KeyEvent ä MouseEvent

Types of Java Event Listeners ä ActionListener ä AdjustmentListener ä ItemListener ä ComponentListener ä ContainerListener ä FocusListener ä WindowListener ä KeyListener ä MouseListener ä MouseMotionListener ä TextListener

Event Adapters ä Convenience classes that implement event listener interfaces ä Provide “stubs” for methods you don’t want to implement yourself ä You just override the implementation of event handlers of interest ä For example, the WindowListener interface declares windowActivated, windowClosed, windowClosing, windowDeactivated, windowIconified, windowDeiconified, windowOpened methods that the WindowAdapter implements

Example of an Event Adapter public class MyFrame extends JFrame {... // inside of a constructor, for example addWindowListener (new WindowAdapter () { public void windowClosing (WindowEvent evt) { public void windowClosing (WindowEvent evt) { System.exit (0); System.exit (0); } } ); // end of addWindowListener call ); // end of addWindowListener call...}

Processing GUI Events in Java ä Implement an event handler ä Define a class that implements an event-listener interface (i.e., defines the appropriate event handling method(s)) ä Create an instance of the class ä Register the event handler with the widget, e.g.: ä addActionListener() ä addItemListener()

Another Example // define a public class MyFrame that extends JFrame private JButton myButton1; private boolean state1; // instance variables // in the constructor myButton1 = new JButton(“Press Me”); ButtonHandler handler = new ButtonHandler(); myButton1.addActionListener (handler); addWindowListener (new WindowAdapter () { public void windowClosing (WindowEvent evt) {System.exit(0); } public void windowClosing (WindowEvent evt) {System.exit(0); } } ); ); // elsewhere in the class, define method main for class MyFrame public static void main(String args[]) { MyFrame myFrame1 = new MyFrame(); MyFrame myFrame1 = new MyFrame(); myFrame1.show(); myFrame1.show();}

Another Example (2) // elsewhere in the class, define an inner class private class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent evt) public void actionPerformed(ActionEvent evt) { if (state1) { if (state1) { myButton1.setText("State2”); myButton1.setText("State2”); state1 = false; state1 = false; } else { else { myButton1.setText("State1”); myButton1.setText("State1”); state1 = true; state1 = true; } }

java.awt.Component ä The parent class for almost all user interface widgets ä Includes methods for: ä Adding various event listeners ä Getting/setting location & size ä Handling text (fonts,...) ä Painting a Graphics object ä Processing focus, visibility, colors, cursors,...

javax.swing.JComponent ä Parent class for most Swing components ä Supports different look-and-feels ä Mechanisms for tool tips, mnemonics, internationalization, borders, scrolling,...

java.awt.Container ä A collection of (holder for) other widgets ä You usually work with one of its subclasses (like JFrame) ä You add() other widgets to a container ä Can have a layout policy ä Set before add widgets ä Only one layout policy at a time ä Has benefits and disadvantages

Layout Policies ä Set with setLayout() method ä FlowLayout (left to right, top to bottom) ä BorderLayout (5 regions) ä GridLayout (arbitrary sized matrix) ä Others (Box, Card, GridBag,...)

java.awt.Frame ä Top-level window (implemented via native window) ä Has a title ä Can have a menubar ä Controls cursors

javax.swing.JFrame ä Extends Frame ä Platform-neutral window ä Children must be added to the content pane (Container) ä Has default close operation

javax.swing.JPanel ä Panels are sub-containers (containers within containers) ä Used to organize user interface into major sections ä Fairly lightweight

javax.swing.JLabel ä Extends JComponent ä Non-editable text and/or Image ä Generally does not change even under program control ä Often used with a text entry field ä Very important human factors element

javax.swing.JButton ä Extends AbstractButton ä Implements a simple user selection paradigm ä Typically has a title ä Can also have an Icon ä Several types ä Simple push button (command button) ä Select one of many radio button ä Two-state check box

javax.swing.JTextField ä Extends JTextComponent ä Basic one-line text entry ä Can be uneditable, typically editable ä Can set size by number of characters ä Includes text processing mechanisms (font, alignment,...)

javax.swing.JList ä Extends JComponent ä Multiple or sing-select, potentially scrolla ble, list ä Need to embed in a JScrollPane to get scrollbars ä Includes mechanisms to set, clear selected item(s), set number of visible items, modify colors, convert point to list index, modify list data,... ä Different selection modes (single, single contiguous, multiple discrete)