Lecture 8:Event Driven Programming Michael Hsu CSULA.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 16 JavaFX UI Controls and Multimedia.
Advertisements

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 15 Event-Driven Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Programming Based on Events
Event-Driven Programming
CS 112 GUI 06 May 2008 Bilkent. Java GUI API Containers: ◦ contain other GUI components. E.g, Window, Panel, Applet, Frame Dialog. Components: ◦ Buttons,
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 16 Event-Driven Programming.
1 Event-Driven Programming Just like designing GUIs, you also will probably not write Java code like the program examples given in these notes. You will.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
Visual Basic Chapter 1 Mr. Wangler.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
Chapter 8: Writing Graphical User Interfaces
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Dale Roberts GUI Programming using Java - GUI Components Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Topics Introduction Scene Graphs
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 – Graphical User Interfaces Java Foundations: Introduction to Programming.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Class Builder Tutorial Presented By- Amit Singh & Sylendra Prasad.
Object-Oriented Application Development Using VB.NET 1 Chapter 10 VB.NET GUI Components Overview.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Creating User Interfaces Chapter 13 CSCI CSCI 1302 – Creating User Interfaces2 Outline Introduction Common Features of Swing GUI Components Buttons.
Creating User Interfaces Event-Driven Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Object Oriented Programming in Java Habib Rostami Lecture 10.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Chapter 14 Event-Driven Programming
Lecture 8:Event Driven Programming
Chapter 15 Event-Driven Programming and Animations
Java Events. Java Events Important definitions Overridden method Class vs. abstract class vs. interface Event Handling Definition Main components Windows.
Chapter Topics 15.1 Graphical User Interfaces
Chapter 8: Writing Graphical User Interfaces
Chapter 16 JavaFX UI Controls and Multimedia
Chap 7. Building Java Graphical User Interfaces
Chapter 16 JavaFX UI Controls and Multimedia
Graphical User Interfaces -- Introduction
Chapter 7 Creating User Interfaces
Recall: Timeline Class
Chapter 15 Event-Driven Programming and Animations
CISC124 Last Quiz this week. Topics listed in last Tuesday’s lecture.
Chapter 16 Event-Driven Programming
Chapter 15 Event-Driven Programming and Animations Part 1
Chapter 16 JavaFX UI Controls and Multimedia
Chapter 15 Event-Driven Programming and Animations Part 2
CMPE212 – Reminders Assignment 5, a JavaFX GUI, due next Friday.
Chapter 16 JavaFX UI Controls and Multimedia
Presentation transcript:

Lecture 8:Event Driven Programming Michael Hsu CSULA

Review: External Stylesheets in JavaFX  This is not a HTML/CSS class, so I’m not going to ask you to memorize all the rules.  You can set styles in code:  hbox.setStyle("-fx-background-color: #336699;");  Quickly gets out of hand  What if you want to switch look and feel without changing your code?  Adding CSS file from same package: scene.getStylesheets().add(HelloWorld.class.getResource("example.css").toExternalForm());  Adding CSS file from other packages: scene.getStylesheets().add("/css/example.css");

CSS JavaFX Tips  Quickly style the entire scene: customize.root class.root{ -fx-font-size: 16pt; -fx-font-family: "Courier New"; -fx-base: rgb(132, 145, 47); -fx-background: rgb(225, 228, 203); }  Override Default Style.button{ -fx-text-fill: rgb(49, 89, 23); -fx-border-color: rgb(49, 89, 23); -fx-border-radius: 5; -fx-padding: ; }

Creating your own CSS class  1. In external file:.button1{ -fx-text-fill: #006464; -fx-background-color: #DFB951; -fx-border-radius: 20; -fx-background-radius: 20; -fx-padding: 5; }  2. In your code: Button buttonAccept = new Button("Accept"); buttonAccept.getStyleClass().add("button1");

Using IDs  1. In external file: #font-button { -fx-font: bold italic 20pt "Arial"; -fx-effect: dropshadow( one-pass-box, black, 8, 0.0, 2, 0 ); }  2. In your code: Button buttonFont = new Button("Font"); buttonFont.setId("font-button");

Further Reading  JavaFX CSS tutorial:   JavaFX CSS reference:   MDN CSS tutorial/documentation: 

Procedural vs. Event-Driven Programming  Procedural programming is executed in procedural order.  In event-driven programming, code is executed upon activation of events.  You probably already did some event driven programming in CS201:  JOptionPane.showInputDialog

8 8 Handling GUI Events Source object (e.g., button) Listener object contains a method for processing the event.

Events  An event can be defined as a type of signal to the program that something has happened.  The event is generated by external user actions such as mouse movements, mouse clicks, or keystrokes. F9F9

10 Event Classes

Event Information  An event object contains whatever properties are pertinent to the event.  You can identify the source object of the event using the getSource() instance method in the EventObject class.  The subclasses of EventObject deal with special types of events, such as button actions, window events, mouse movements, and keystrokes.

12 Selected User Actions and Handlers

13 Example: Controlling Circle Now let us consider to write a program that uses two buttons to control the size of a circle.

14 Inner Class Listeners  A listener class is designed specifically to create a listener object for a GUI component (e.g., a button).  It will not be shared by other applications. So, it is appropriate to define the listener class inside the frame class as an inner class.

15 Inner Classes, cont.

16 Inner Classes (cont.) Inner classes can make programs simple and concise. An inner class supports the work of its containing outer class and is compiled into a class named OuterClassName$InnerClassName.class. For example, the inner class InnerClass in OuterClass is compiled into OuterClass$InnerClass.class.

Inner Classes (cont.)  An inner class can be declared public, protected, or private subject to the same visibility rules applied to a member of the class.  An inner class can be declared static. A static inner class can be accessed using the outer class name. A static inner class cannot access nonstatic members of the outer class

18 Anonymous Inner Classes  An anonymous inner class must always extend a superclass or implement an interface, but it cannot have an explicit extends or implements clause.  An anonymous inner class must implement all the abstract methods in the superclass or in the interface.  An anonymous inner class always uses the no-arg constructor from its superclass to create an instance. If an anonymous inner class implements an interface, the constructor is Object().  An anonymous inner class is compiled into a class named OuterClassName$n.class. For example, if the outer class Test has two anonymous inner classes, these two classes are compiled into Test$1.class and Test$2.class.

19 Anonymous Inner Classes (cont.) Inner class listeners can be shortened using anonymous inner classes. An anonymous inner class is an inner class without a name. It combines declaring an inner class and creating an instance of the class in one step. An anonymous inner class is declared as follows: new SuperClassName/InterfaceName() { // Implement or override methods in superclass or interface // Other methods if necessary }

20 Anonymous Inner Classes (cont.)

21 Simplifying Event Handing Using Lambda Expressions Lambda expression is a new feature in Java 8. Lambda expressions can be viewed as an anonymous method with a concise syntax. For example, the following code in (a) can be greatly simplified using a lambda expression in (b) in three lines.

22 Basic Syntax for a Lambda Expression The basic syntax for a lambda expression is either (type1 param1, type2 param2,...) -> expression or (type1 param1, type2 param2,...) -> { statements; } The data type for a parameter may be explicitly declared or implicitly inferred by the compiler. The parentheses can be omitted if there is only one parameter without an explicit data type.

23 Single Abstract Method Interface (SAM)  The statements in the lambda expression is all for that method.  If it contains multiple methods, the compiler will not be able to compile the lambda expression.  For the compiler to understand lambda expressions, the interface must contain exactly one abstract method. Such an interface is known as a functional interface, or a Single Abstract Method (SAM) interface.

24 Frequently Used UI Controls The prefixes lbl, bt, chk, rb, tf, pf, ta, cbo, lv, scb, sld, and mp are used to name reference variables for Label, Button, CheckBox, RadioButton, TextField, PasswordField, TextArea, ComboBox, ListView, ScrollBar, Slider, and MediaPlayer.

25 Labeled A label is a display area for a short text, a node, or both. It is often used to label other controls (usually text fields). Labels and buttons share many common properties. These common properties are defined in the Labeled class.

26 Label The Label class defines labels. LabelWithGraphic

27 ButtonBase and Button A button is a control that triggers an action event when clicked. JavaFX provides regular buttons, toggle buttons, check box buttons, and radio buttons. The common features of these buttons are defined in ButtonBase and Labeled classes.

28 Button Example ButtonDemo

29 CheckBox A CheckBox is used for the user to make a selection. Like Button, CheckBox inherits all the properties such as onAction, text, graphic, alignment, graphicTextGap, textFill, contentDisplay from ButtonBase and Labeled.

30 CheckBox Example CheckBoxDemo

31 RadioButton Radio buttons, also known as option buttons, enable you to choose a single item from a group of choices. In appearance radio buttons resemble check boxes, but check boxes display a square that is either checked or blank, whereas radio buttons display a circle that is either filled (if selected) or blank (if not selected).

32 RadioButton Example RadioButtonDemo

33 TextField A text field can be used to enter or display a string. TextField is a subclass of TextInputControl.

34 TextField Example TextFieldDemo

35 TextArea A TextArea enables the user to enter multiple lines of text.

36 TextArea Example TextAreaDemoDescriptionPane

37 ComboBox A combo box, also known as a choice list or drop-down list, contains a list of items from which the user can choose.

38 ComboBox Example This example lets users view an image and a description of a country's flag by selecting the country from a combo box. ComboBoxDemo

FileChooser  Reference:  How to use:  1. Get java.io.File from FileChooser: FileChooser fileChooser = new FileChooser(); fileChooser.setTitle("Open Resource File"); File file = fileChooser.showOpenDialog(primaryStage);  2. get URI of file and create path: Path filePath = Paths.get(file.toURI());