GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Slides:



Advertisements
Similar presentations
Event Handling.
Advertisements

Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns.
For IST410 Students only Events-1 Event Handling.
GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
Java Programming Chapter 10 Graphical User Interfaces.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington User Interface COMP 112 #30.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/16) Internationalization and Locales Joel Adams and Jeremy Frens Calvin.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 1 Lecture 5 – GUI Programming r Inner classes  this and super r Layout r Reading: m.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
LAB 1CSIS04021 Briefing on Assignment One & RMI Programming February 13, 2007.
Data Structures and Java CS 105. L7: Java Slide 2 Data structure Data structure defined: A systematic way of organizing and accessing data Examples Dictionary:
Java Programming: Guided Learning with Early Objects
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Chapter 7: Pinball Game Construction Kit. Vectors Example of a “collection class” Must “import java.util.Vector” More flexible than arrays: will grow.
 Definition: An event is an object thrown in response to a user of programmatic action  Definition: A listener is a series of methods that executes in.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
CS 151: Object-Oriented Design October 31 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Creating a GUI Class An example of class design using inheritance and interfaces.
Interfaces Describe what classes should do, without specifying how they should do it Not a class, but a set of requirements for classes that want to conform.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
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.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Sep 181 Example Program DemoTranslateEnglishGUI.java.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
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:
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
A Quick Java Swing Tutorial
Events and Event Handling
Lecture 8 Object Oriented Programming Using Java
“Form Ever Follows Function” Louis Henri Sullivan
A Quick Java Swing Tutorial
Programming in Java Event Handling
Ellen Walker Hiram College
GUI Programming III: Events
Chapter 13: Advanced GUIs and Graphics
ITEC324 Principle of CS III
A Quick Java Swing Tutorial
Events, Event Handlers, and Threads
Final project.
ITEC324 Principle of CS III
Advanced GUIs and Graphics
Chapter 6 Inheritance.
Presentation transcript:

GUI

Swing Class Hierarchy

Swing Components

Swing Conatiners  JFrame – top-level window to store components

Swing Conatiners JPanel – container; can be embedded in JFrame

Layouts  FlowLayout  arranges elements in a row  elements centered by default within container  GridLayout  subdivides container into cells of identical sizes  components take up all available space of a cell  BorderLayout  subdivides container into 5 areas: N, S, E, W, Center

Layouts 3x3 GridLayout4x1 GridLayoutBorderLayout FlowLayout used to place the 3 panels in the Jframe.

Listeners  Process events  ActionListener (JButton, Timer, JComboBox)‏  ChangeListener (JSlider)‏  MouseListener, MouseMotionListener  Listeners are interfaces; must implement ALL specified methods  ActionListener: void actionPerformed(ActionEvent e)‏  ChangeListener: void stateChanged(ChangeEvent e)‏  MouseListener: void mouseClicked(MouseEvent e) void mousePressed(MouseEvent e) void mouseReleased(MouseEvent e) void mouseEntered(MouseEvent e) void mouseExited(MouseEvent e)‏  MouseMotionListener: void mouseMoved(MouseEvent e) void mouseDragged(MouseEvent e)

Adapter classes  Convenience classes  server as intermediaries between available interfaces (e.g. listeners) and the user defined classes (e.g. listeners)  make it possible to implement only the “important” methods

Adapter classes  Convenience classes  server as intermediaries between available interfaces (e.g. listeners) and the user defined classes (e.g. listeners)  make it possible to implement only the “important” methods abstract class MouseAdapter implements MouseListener, MouseMotionListener { void mousePressed(MouseEvent e) { // empty body } void mouseReleased(MouseEvent e) { // empty body } void mouseEntered(MouseEvent e) { // empty body } void mouseExited(MouseEvent e) { // empty body } voidmouseMoved(MouseEvent e) { // empty body } voidmouseDragged(MouseEvent e) { // empty body } } MouseListener methods MouseMotionListener methods

Adapter classes  Convenience classes  server as intermediaries between available interfaces (e.g. listeners) and the user defined classes (e.g. listeners)  make it possible to implement only the “important” methods abstract class MouseAdapter implements MouseListener, MouseMotionListener { abstract void mousePressed(MouseEvent e); abstract void mouseReleased(MouseEvent e); abstract void mouseEntered(MouseEvent e); abstract void mouseExited(MouseEvent e); abstract voidmouseMoved(MouseEvent e); asbtract voidmouseDragged(MouseEvent e); }

File IO

 PrintWriter – for writing to file; same methods as in System.out  Pretend that all output goes to screen try { PrintWriter output = new PrintWriter(“input-file.txt”); output.println(“Hello”); output.printl(42); output.println(“3.1459”); output.close(); } catch (Exception e) { // report error }

File IO  Scanner – for reading from file; same as in CS111  Contents is viewed as a stream of characters  Reading stops as soon as appropriate token found; otherwise fails try { Scanner input = new Scanner(new File(“input-file.txt”)); String word = input.next(); int answer = input.nextInt(); double pi = input.nextDouble(); input.close(); } catch (Exception e) { // report error }

Command-Line

Command Line Arguments  Make it possible to send data to the program on execution public static void main(String[] args)‏ { System.out.println(“Parameters given on start-up:”); System.out.println(“Number of params: “ + args.length); for (int i = 0; i < args.length; i++) { System.out.println(args[i]); // i-th parameter value }

Exceptions

 Mechanism for handling unexpected conditions (errors)‏  Force the programmer to handle error conditions  Allow for separating the logic of the code from error-handling  Sometimes no other option to report the value:  constructor  minElement, maxElement  Example – see FileIO

Exceptions  Can create our own type of exception (should inherit from Exception)‏ class EmptyArrayException extends Exception { public void EmptyArrayException()‏ { super(); } public void EmptyArrayException(String message)‏ { super(message); }

Exceptions  Example of our own Exception --- throw/throws int minElement(int[] numbers) throws EmptyArrayException { // empty array --- throw an exception if (numbers.length == 0)‏ { throw EmptyArrayException(“Empty array given”); } // //... compute smallest element... // }

extends vs. implements class vs. interface multiple inheritance

 An interface specifies a collection of methods  An interface does not have data members or code for methods  A class that implements the interface must provide code (implementation) for all methods listed in the interface interface RemoteControllable { public void play(); public void stop(); public void ffwd(); } class VCR implements RemoteControllable { // must provide code for all methods in RemoteControllable } class DVD implements RemoteControllable { // must provide code for all methods in RemoteControllable } Interfaces

Multiple Inheritance String name; //species void setName(String n) { } String name; // pet’s name void setName(String n) { } Class MammalClass Pet Class Cat

Multiple Inheritance String name; //species void setName(String n) { } String name; // pet’s name void setName(String n) { } Class MammalClass Pet Which name is inherited? Which setName() is inherited? Class Cat

 Complex rules required to disambiguate in multiple inheritance  Java does not support multiple inheritance; C++ does Multiple Inheritance String name; //species void setName(String n) { } String name; // pet’s name void setName(String n) { } Class MammalClass Pet Which name is inherited? Which setName() is inherited? Class Cat

 What if we still want a Cat to behave like a Mammal and Pet interface Mammal { // all methods (behaviors) common to mammals // no code is specified, just the behavior names (methods) } class Pet { // description of generic pet } class Cat extends Pet implements Mammal { // has all behaviors of a Pet – could override some // must implement all behaviors of Mammal } Multiple Inheritance

 Can now use Cat objects anywhere Mammal behaviors required or where Pet objects are required public void hunt(Mammal predator, Mammal prey) { // do something; could send a Cat as // either prey or predator } public void doTricks(Pet pet) { // do something; could send a Cat for pet } Multiple Inheritance

 A Java class can only extend from one other class (single inheritance)  A Java class can implement multiple interfaces – can ambiguity arise? Multiple Interfaces Implementation

 A Java class can only extend from one other class (single inheritance)  A Java class can implement multiple interfaces – no ambiguity since  an interface cannot have data members  an interface cannot have code (implementation) for methods Multiple Interfaces Implementation