Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 8:Event Driven Programming

Similar presentations


Presentation on theme: "Lecture 8:Event Driven Programming"— Presentation transcript:

1 Lecture 8:Event Driven Programming
Michael Hsu CSULA

2 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");

3 CSS JavaFX Tips Quickly style the entire scene: customize .root class
-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: ;

4 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");

5 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");

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

7 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 Handling GUI Events Source object (e.g., button)
Listener object contains a method for processing the event. 8

9 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.

10 Event Classes

11 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.

17 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 Label The Label class defines labels. LabelWithGraphic

26 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.

27 Button Example ButtonDemo

28 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.

29 CheckBox Example CheckBoxDemo

30 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).

31 RadioButton Example RadioButtonDemo

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

33 TextField Example TextFieldDemo

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

35 TextArea Example DescriptionPane TextAreaDemo

36 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.

37 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

38 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());


Download ppt "Lecture 8:Event Driven Programming"

Similar presentations


Ads by Google