Introduction to JavaFX Tecniche di Programmazione
Summary 1. About and History 2. Basic concepts 3. Minimal JavaFX Application 4. Resources 2
About and History Introduction to JavaFX
GUI in Java Graphic framework available in Java Swing Extremely powerful, many extensions available Complex to master, requires low-level handling Hard to create visually pleasing applications Alternatives available Most notable: SWT (Eclipse) Still cumbersome to master On a different Universe, web-based user interfaces became nicer and faster to create 4
JavaFX 1.0 forget it JavaFX 1 and JavaFX 2 are completely different Version 1 relied on a scripting language to describe scenes, with hooks to activate Java code JavaFX 1.x is now deprecated 5
JavaFX 2.x Redesigned from scratch The JavaFX 2.x framework is entirely written in Java For visual layout, an XML file may also be used (called FXML) Graphic appearance borrows from web-standard CSS style sheets UI programming is based on easy to handle events and bindings Oracle plans to deprecate Swing in favor of JavaFX 2 6
Getting and running JavaFX JavaFX is already included in Oracle JDK 7 Not in JDK 6.x Not in OpenJDK (beware, Linux users!) Recommended: JavaFX Scene Builder Eclipse: e(fx)clipse plugin, available in the Eclipse Marketplace Download links are in the course webpage 7
Basic concepts Introduction to JavaFX
Key concepts in JavaFX Stage: where the application will be displayed (e.g., a Windows window) Scene: one container of Nodes that compose one page of your application Node: an element in the Scene, with a visual appearance and an interactive behavior. Nodes may be hierarchically nested My best friend is the JavaFX JavaDoc API http://docs.oracle.com/javafx/2/api/index.html 9
Some Nodes (UI Form controls) 10
Some Parent Nodes (Layout panes) BorderPane (5-areas) Hbox, Vbox (linear sequence) StackPane (overlay all children) GridPane (row x columns) FlowPane (flowing boxes, wrap around) TilePane (flowpane with equally sized boxes) AnchorPane (magnetically attach nodes at corners or sides) 11
Some Nodes (Charts) 12
Nodes family 13 javafx.scene.node Parent Control ChoiceBox ComboBoxBase ColorPicker ComboBox Labeled ButtonBase Button CheckBox MenuButton ToggleButton Cell Label TitledPane ListView MenuBar Separator Slider TabPane TextInputControl TextArea TextField ToolBar TreeView Group Region Axis Chart Pane AnchorPane BorderPane FlowPane GridPane HBox StackPane TilePane VBox WebView Shape Arc Circle Line Polygon Polyline Rectangle Text Canvas Imageview Focus on Panes and Controls
And more coming 14
Empty JavaFX window public class Main extends Application { @Override public void start(stage stage) { Group root = new Group(); // the root is Group or Pane Scene scene = new Scene(root, 500, 500, Color.BLACK); stage.settitle("javafx Demo"); stage.setscene(scene); stage.show(); } } public static void main(string[] args) { launch(args); } 15
Adding some shape public class Main extends Application { @Override public void start(stage stage) { Group root = new Group(); Scene scene = new Scene(root, 500, 500, Color.BLACK); stage.settitle("javafx Demo"); Rectangle r = new Rectangle(25,25,250,250); r.setfill(color.blue); root.getchildren().add(r); } stage.setscene(scene); stage.show(); } 16
How to add scene content In Java code By creating and adding new Node subclasses Standard way, in Java (boring and error-prone) By using node Builder classes Programming pattern, later on In FXML By writing XML directly By using the Scene Editor And loading the FXML into the application 17
JavaFX Scene Builder 18
... FXML fragment <HBox id="hbox" alignment="center" spacing="15.0" AnchorPane.rightAnchor="23.0" AnchorPane.topAnchor="22.0"> <children> <Button id="button1" fx:id="newissue" onaction="#newissuefired text="new" /> <Button id="button1" fx:id="saveissue" onaction="#saveissuefired text="save" /> <Button id="button1" fx:id="deleteissue" onaction="#deleteissuefired" text="delete" /> </children> </HBox> <ImageView id="issuetrackinglite" layoutx="14.0" layouty="20.0"> <image> <Image url="@issuetrackinglite.png" preserveratio="true" smooth="true" /> </image> </ImageView>... 19
Building a scene from FXML public void start(stage stage) throws Exception { Parent root = FXMLLoader.load( getclass().getresource("circle.fxml")); } stage.settitle("circle Demo"); stage.setscene(new Scene(root, 500, 150)); stage.show(); 20
Resources Introduction to JavaFX
Resources Official http://www.oracle.com/us/technologies/java/fx/overview/index. html http://www.oracle.com/technetwork/java/javafx/overview/index.html Documents http://docs.oracle.com/javafx/ http://docs.oracle.com/javafx/2/api/index.html Blogs http://fxexperience.com/ http://www.learnjavafx.typepad.com/weblog/ http://community.java.net/community/javafx 22
Licenza d uso Queste diapositive sono distribuite con licenza Creative Commons Attribuzione - Non commerciale - Condividi allo stesso modo (CC BY-NC-SA) Sei libero: di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico, rappresentare, eseguire e recitare quest'opera di modificare quest'opera Alle seguenti condizioni: Attribuzione Devi attribuire la paternità dell'opera agli autori originali e in modo tale da non suggerire che essi avallino te o il modo in cui tu usi l'opera. Non commerciale Non puoi usare quest'opera per fini commerciali. Condividi allo stesso modo Se alteri o trasformi quest'opera, o se la usi per crearne un'altra, puoi distribuire l'opera risultante solo con una licenza identica o equivalente a questa. http://creativecommons.org/licenses/by-nc-sa/3.0/ 23