+ All Categories
Home > Documents > CISC 3120 C15: JavaFX: Styling, FXML, and...

CISC 3120 C15: JavaFX: Styling, FXML, and...

Date post: 15-Jul-2019
Category:
Upload: vuongnga
View: 213 times
Download: 0 times
Share this document with a friend
25
CISC 3120 C15: JavaFX: Styling, FXML, and MVC Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/19/2017 1 CUNY | Brooklyn College
Transcript
Page 1: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

CISC 3120

C15: JavaFX: Styling, FXML, and MVC

Hui Chen

Department of Computer & Information Science

CUNY Brooklyn College

10/19/2017 1CUNY | Brooklyn College

Page 2: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

Outline

• Recap and issues

• Styling user interface with CSS

• FXML and Model-View-Controller pattern

• If time permits

• Canvas, charts, and others

• Interoperate with Swing

• Assignments

• Project 3 (group activity)

10/19/2017 CUNY | Brooklyn College 2

Page 3: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

JavaFX Cascading Style Sheets

• Control appearance of JavaFX interface using Cascading Style Sheets

• Cascading Style Sheets (CSS)

• A World-Wide-Web Consortium (W3C) standard

• Originally designed as a simple mechanism for adding style (e.g., fonts, colors, spacing) to Web documents

• See https://www.w3.org/Style/CSS/

• CSS level 1, 2, and 3 (some still under development)

• JavaFX CSS (JavaFX 8)

• Based on W3C CSS level 2.1 with some addition on current work on CSS level 3

• Aimed at providing a uniform method to style both desktop and web applications

10/19/2017 CUNY | Brooklyn College 3

Page 4: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

An Example of JavaFX CSS

.root {

-fx-font-size: 16pt;

-fx-font-family: "Courier New";

-fx-base: rgb(132, 145, 47);

-fx-background: rgb(225, 228, 203);

-fx-background-image: url("background.jpg");

-fx-background-repeat: no-repeat;

-fx-background-size: cover;

}

10/19/2017 CUNY | Brooklyn College 4

Selector

Styles in {}

A style is written as a property and value pair, and the property name and its value is separated by a “:”, and ended with a “;”.

JavaFX property names are prefixed with a vendor extension of "-fx-".

Page 5: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

Apply Styles

• Styles are applied (but not necessarily selected for) to Nodes in the Scene-graph

• First applied to the parent, then to its children

• A node is styled after it is added to the scene graph.

• A node is re-styled

• when the following changes made to the node's pseudo-class state, style-class, id, inline style, or parent

• Pseudo-class state: e.g., MouseEvent.MOUSE_ENTERED

• When stylesheets are added to or removed from the scene.

10/19/2017 CUNY | Brooklyn College 5

Page 6: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

CSS Selectors

• CSS selectors are used to match styles to scene-graph nodes

• Type selector

• Class selector

• ID selector

10/19/2017 CUNY | Brooklyn College 6

Page 7: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

Type Selector

• Select based on type name returned by Node’s getTypeSelector method

• Analogous to a CSS type selector

• See style and code example in

• JavaFXCssStyledCsQuoteApp

10/19/2017 CUNY | Brooklyn College 7

Page 8: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

Class Selector

• Select based on the value of the styleClassproperty of the Node

• A Node can have multiple style classes

• Analogous to a CSS class selector

• See style and code example in

• JavaFXCssStyledCsQuoteApp

10/19/2017 CUNY | Brooklyn College 8

Page 9: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

ID Selector

• Select based on the ID of the Node

• The ID of a Node can be set using Node’s setIdmethod

• ID is should be unique

• Analogous to a CSS ID selector

• See style and code example in

• JavaFXCssStyledCsQuoteApp

10/19/2017 CUNY | Brooklyn College 9

Page 10: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

Context Selector

• Selection based on contextual information

• Example:

• #brooklyn-orange-next-quote Text { … }

• matches a Node whose type name is “Text” and the Node is a descendent of the Node whose ID is #brooklyn-orange-next-quote

• See CSS 3 Selectors for more

• https://www.w3.org/TR/css3-selectors/

10/19/2017 CUNY | Brooklyn College 10

Page 11: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

Swing & JavaFX

• Swing is a successful toolkit for more than a decade

• Why JavaFX

• To provide applications with such sophisticated GUI features

• Smooth animation, web views, audio and video playback

• Styles based on Cascading Style Sheets (CSS)

10/19/2017 CUNY | Brooklyn College 11

Page 12: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

User Interface Design

• Thus far, the interface design is tied to the code.

• Is there any other way to do it?

10/19/2017 CUNY | Brooklyn College 12

Page 13: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

User Interface Design with FXML

• FXML

• XML-based language

• XML = Extensible Markup Language

• Help build a user interface separated from the application logic

10/19/2017 CUNY | Brooklyn College 13

Page 14: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

Model-View-Controller

• Key separation of concerns: view and controller depend on model, but model depends on neither.

10/19/2017 CUNY | Brooklyn College 14

Model

Controller

View

Page 15: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

Model-View-Controller

• It separates the three,

• (Model) the modeling of the domain

• (View) the presentation,

• (Controller) and the actions based on user input into three separate classes

• A fundamental design pattern for the separation of user interface logic from business logic.

10/19/2017 CUNY | Brooklyn College 15

Page 16: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

Model

• Manages the behavior and data of the application domain

• Responds to requests for information about its state (usually from the view)

• Responds to instructions to change state (usually from the controller).

10/19/2017 CUNY | Brooklyn College 16

Page 17: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

View

• Manages the display of information.

10/19/2017 CUNY | Brooklyn College 17

Page 18: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

Controller

• Interprets the mouse and keyboard inputs from the user

• Inform the model and/or the view to change as appropriate.

10/19/2017 CUNY | Brooklyn College 18

Page 19: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

Computer Quote App

• Model (or Domain)

• Computer science authors and what they said

• View

• The interface shows the quotes

• Controller

• Intercept users’ mouse clicks

• Inform model (or domain) about quote to display

• Inform view to update the quote to be displayed

10/19/2017 CUNY | Brooklyn College 19

Page 20: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

Use Eclipse for JavaFX FXML Project• If from scratch

• Download and install JavaFX Scene Builder 2.0

• Oracle does not offer the binary any more

• Source code is distributed with the OpenJFX project

• Download & install from a 3rd party provider

• Create a Maven project

• Create Controller class (always name it as a Controller)

• Use @FXML to annotate fields and methods

• Create FXML file

• You can open & edit it using the Scene Builder 2.0

• Set handler

10/19/2017 CUNY | Brooklyn College 20

Page 21: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

Example: JavaFXFXMLCsQuoteApp

• Define the Model

• ComputerScienceQuoteDataModel.java

• Define the View

• fxml_mainscene.fxml

• Define the Controller

• FXMLGridViewController.java

10/19/2017 CUNY | Brooklyn College 21

Page 22: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

Example: JavaFXFXMLCsQuoteApp• Entry Point of the Application

private final static String APP_TITLE = "Quotations in Computer Science";

private final static String MAIN_SCENE_FXML = "fxml_mainscene.fxml";

@Override

public void start(Stage primaryStage) throws IOException {

Pane mainPane =

(Pane)FXMLLoader.load(getClass().getResource(MAIN_SCENE_FXML));

Scene mainScene = new Scene(mainPane);

primaryStage.setTitle(APP_TITLE);

primaryStage.setScene(mainScene);

primaryStage.show();

}

10/19/2017 CUNY | Brooklyn College 22

Page 23: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

JavaFX & Swing Interoperability

• Swing applications can use JavaFX

• JavaFX applications can use Swing

10/19/2017 CUNY | Brooklyn College 23

Page 24: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

Grow your skills & knowledge

• CISC 3620 Computer Graphics

• 2D and 3D graphics

• CISC 3320 Operating Systems

• Concurrency, processes, and threads

10/19/2017 CUNY | Brooklyn College 24

Page 25: CISC 3120 C15: JavaFX: Styling, FXML, and MVChuichen-cs.github.io/course/CISC3120/17FA/lecture/cisc3120_c15.pdf · Model-View-Controller •It separates the three, •(Model) the

Assignments

• Practice

• Project 3

10/19/2017 CUNY | Brooklyn College 25


Recommended