Intro To JavaFX - GitHub Pages

2y ago
16 Views
2 Downloads
1.45 MB
31 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Karl Gosselin
Transcription

Introduction to JavaFX

JavaFX is a graphics framework for creatingdesktop and mobile apps.JavaFX interfaces can be defined declaratively(in XML) instead of Java code. (In Swing, theUI is defined entirely in code.)Example of both ways shown later.

Some ConceptsA JavaFX application contains a Windowthat contains a Stagethat contains a graph (tree) of Componentsand Layoutsappearance is controlled by properties you can set:size, color, background, spacing (social distancing), .

U.I. is Presented on a Stage

The Stage Contains a Scenestage.setScene( scene );

Scene has Components & Layoutsscene.setLayout( livingRoomLayout );scene.getChildren().add( Sofa );

What You Need to Know1. How do I get a Stage?2. What are the Layouts and Containers?3. How do I use Components?4. What Properties can I set? (appearance)5. How to respond to Events?

Create this UI in Code

Structure of JavaFX App (main)public class HelloFX extends Application {public static void main(String[] args) {launch(args);}@Overridepublic void start(Stage primaryStage) {// Create a container as root node in the SceneFlowPane root new FlowPane();// Set appearance of container (spacing, alignment)// Add components to the container)// Show the scene graph on the StageprimaryStage.setScene(new Scene(root));primaryStage.show();}

Define Container & ComponentsLabel (read-only)TextField (input)FlowPane - components "flow" left-to-rightButton

Add Componentspublic void start(Stage primaryStage) {FlowPane root new FlowPane();// Set appearance of container// Add components to the container)Label prompt new Label("Who are you?");TextField nameField new TextField();Button button new Button("Greet ;

View ItLooks ugly.

Run-time AnnoyanceWhen you run a JavaFX application with Java 11 youmay get this message:Error: JavaFX runtime components aremissing, and are required to run this.This relates to modules in Java 9 . Here's a fix:Cmd line:java --module-path /path/to/javafx/lib--add-modules javafx.base,javafx.controlsIDE: Add --module-path and --add-modules to VM args.

Java 8 - Retrograde SolutionJava 8 includes JavaFX in the JDK (no external Jars)and does not use modules.You can add JDK 8 to your system and configure it inEclipse or IntelliJ, and maybe in VS Code.You choose which IDE (JDK8, JDK11, etc.) for eachproject.You must be careful to run from command line using Java8 "java" command, too.Otherwise JavaFX classes will not be found.

Improve Appearance using PropertiesEvery control has properties you can set that effect itsappearance. Modify the FlowPane:FlowPane root new FlowPane();// Set appearance of p(10.0);root.setPadding(new Insets(10.0));

Where to learn properties?The Oracle JavaFX Tutorial gives many examples ofsetting properties of components.Oracle has downloadable PDF and ePub for.Getting Started with JavaFXJavaFX LayoutsJavaFX UI ControlsUse SceneBuilder (visual layout) -- it's even easier.

Modularizestart() method is getting long.Separate component creation to its own method.public void start(Stage primaryStage) {FlowPane root initComponents();// Show the scene graph on the StageprimaryStage.setScene(new Scene(root));primaryStage.show();

Add BehaviorUI should respond to click on "Greet Me" button.

Events Graphics applications use events.Event is caused by user actions.An event dispatcher notifies interested objects.Event HandlerEventQueueEvent1. ActionEvent2. MouseEvent3. KeyEvent4. WindowEvent.notifyvoid handle( Event event ) {}

Events1. User clicks mouse on a button -- that's an Event.2. JavaFX creates a MouseEvent object.– the MouseEvent describes what happened:which component? which mouse button?3. JavaFX looks for a registered "Event Handler", andcalls it using the ActionEvent as parameter.Event HandlerClick!MyButtonMouseEventnotifyvoid handle( Event event ) {}JavaFX user interface

Adding Event HandlersYou tell JavaFX what events you want to handle, andwhich code to invoke:button.setOnAction( EventHandler ActionEvent ) or button.addEventHandler( eventType, eventHandler )

Write an EventHandlerThis example uses an inner class.Many examples use anonymous class or lambda.class ClickHandlerimplements EventHandler ActionEvent {public void handle(ActionEvent event) {String name nameField.getText().trim();if (name.isEmpty()) {nameField.setPromptText("Please enter a name");}else showDialog("Hello, " name);}}

Access the TextFieldEventHandler needs access to the nameField.Define it as an attribute instead of a local variable.public class HelloFX extends Application {private TextField nameField;public static void main(String[] args) {launch(args);}class ClickHandler implements . {// inner class can access outer class}

Attach Event Handlerprivate void initComponents() {Button button new Button("Greet me");button.setOnAction(new ClickHandler());

showDialogInstead of printing on boring System.out,pop-up an Alert box to greet user.public void showDialog(String message) {Alert alert le("Greetings");alert.setHeaderText( message );// wait for user to dismiss dialogalert.showAndWait();}

Run it

Exercise - Improve the UITODO 1:After greeting theperson, clear the textfrom nameField.TODO 2:If user presses ENTER innameField, also invokeClickHandler, by adding an eventhandler to nameField.You can reuse the sameClickHandler object, don't createanother one.

SceneBuilderVisual tool for creating graphical UI. But first.

Writing a UI in Code is GoodGood to learn the concepts and componentsfirst.For a dynamic UI, it may be necessary to addcomponents using code.

Good TutorialsOracle's JavaFX Tutorial - lots of info about clienttechnologies.htmcode.makery - SceneBuilder tutorial, 7 al/part1/Vojtech Ruzicka JavaFX & SceneBuilder ing-started/also 7 parts. Instructions for IntelliJ.

Suggest a Good Tutorial?If you find a good tutorial site or video(s), post the linkson Google classroom.Or send to me. Posting for everyone is better.

desktop and mobile apps. JavaFX interfaces can be defined declaratively (in XML) instead of Java code. (In Swing, the UI is defined entirely in code.) . The Oracle JavaFX Tutorial gives many examples of setting properties of components. Oracle has downloadable PDF a

Related Documents:

.media - in Default VM Arguments. Note that your JavaFX library location can be different depending on the location that you unzipped. METHOD III: From CMD Prompt: java --module-path "C:\Users\selim\Documents\javafx-sdk-11.0.2\lib" --add-m

This tutorial is a compilation of three documents that were previously delivered with the JavaFX 2.x documentation set: JavaFX Overview, JavaFX Architecture, and Getting Started with JavaFX.

This chapter describes how to add JavaFX co ntent into a Swing application and how to use threads correctly when both Swing and JavaFX content operate within a single application. JavaFX SDK provides the JFXPanel class, which is located in the javafx.embed.swing package and enables you to embed

Oct 10, 2011 · Support for ARM platforms has also been made available with JavaFX 8. JDK for ARM includes the base, graphics and controls components of JavaFX. To install JavaFX install your chosen version of the Java Runtime environment and Java Development kit. Features offered by JavaFX include

Database Operations in JavaFX . At first, part of JavaFX tutorial series, we created a sample JavaFX project, designed the draft version of the UI and set up an Oracle XE database. In this post, we will create controller, model, DAO, and Util classes to do DB operations. Before starting to code, I want to give more information about our example .

What You Will Learn 1 1.1 Background Basics 2 JavaFX Integration 2 1.2 The NetBeans Platform: The Big Picture 3 Module System API 3 . 2.4 Improving the User Experience 63 2.5 Concurrency and Thread Safety 68 . 3.2 Building JavaFX Programs 87 Creating a JavaFX Application 88 Java APIs 88

Swing includes a . JFXPanel. class to embed JavaFX components into a Swing application, and JavaFX includes a . SwingNode. class to embed Swing components into a JavaFX application. Both classes provide corresponding API documentation. Additionally, the official Java documentation site includes an arti

Nazism and the Rise of Hitler 49 In the spring of 1945, a little eleven-year-old German boy called Helmuth was lying in bed when he overheard his parents discussing something in serious tones. His father, a prominent physician, deliberated with his wife whether the time had come to kill the entire family, or if he should commit suicide alone. His father spoke about his fear of revenge, saying .