Advertisement
Guest User

Untitled

a guest
Mar 19th, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.83 KB | None | 0 0
  1. 1 JAVAFX OVERVIEW
  2.  
  3. This chapter provides an overview of the types of applications you can build using JavaFX APIs, where to download the JavaFX libraries, and a high level information about they key JavaVX features being delivered.
  4. JavaFX is a set of graphics and media packages that enables developers to design, create, test, debug, and deploy rich client applications that operate consistently across diverse platforms.
  5.  
  6. - JavaFX Applications
  7. - Availability
  8. - Key Features
  9. - What Can I Build With JavaFX?
  10. - How Do I Run A Sample Application?
  11. - How Do I Run A Sample In an IDE?
  12. - How Do I Create a JavaFX Application?
  13. - Resources
  14.  
  15. See the Understanding the JavaFX Architecture chapter to learn about the JavaFX platform architecture and to get a brief description of the JavaFX APIs for media streaming, web rendering, and user interface styling.
  16.  
  17. JAVAFX APPLICATIONS
  18.  
  19. Since the JavaFX library is written as a Java API, JavaFX application code can reference APIs from any Java library. For example, JavaFX applications can use Java API libraries to access native system capabilities and connect to server-based middelware applications.
  20. The look and feel of JavaFX applications can be customized. Cascading Style Sheets (CSS) separate appearance and style from implementation so that developers can concentrate on coding. Graphic designers can easily customize the appearance and style of the application interface (UI) and the back-end logic, then you can develop the presentation aspects of the UI in the FXML scripting language and use Java code for the application logic. If you prefer to design UIs without writing code, then use JavaFX Scene Builder. As you design the UI, Scene Builder creates FXML markup that can be ported to an Integrated Development Environment (IDE) so that developers can add the business logic.
  21.  
  22. AVAILABILITY
  23.  
  24. The JavaFX APIs are available as a fully integrated feature of the Java SE Runtime Environment (JRE) and the Java Development Kit (JDK). Because the JDK is available for all major desktop platforms (Windows, Mac OS X, and Linux), JavaFX applications compiled to JDK 7 and later also run on all major desktop platforms. Support for ARM platforms has also been made available with JavaFX 8. JDK for ARM includes the base, graphics, and controls components of JavaFX.
  25. The cross-platform compatibility enables a consistent runtime experience for JavaFX applications developers and users. Oracle ensures synchronized releases and updates on all platforms and offers an extensive support program for companies that run mission-critical applications.
  26. On the JDK download page, you can get a zip file of JavaFX sample applications. The sample applications provide many code samples and snippets that show by example how to write JavaFX applications. See "How Do I Run A Sample Application?" for more information.
  27.  
  28. KEY FEATURES
  29.  
  30. The following features are included in the JavaFX 8 and later releases. Items that were introduced in JavaFX 8 release are indicated accordingly:
  31.  
  32. - Java APIs. JavaFX is a Java library that consists of classes and interfaces
  33. that are written in Java code. The APIs are designed to be a friendly
  34. alternative to Java Virtual Machine (Java VM) languages, such as JRuby and
  35. Scala.
  36. - FXML and Scene Builder. FXML is an XML-based declarative markup language for constructing a JavaFX application user interface. A designer can code in
  37. FXML or use JavaFX Scene Builder to interactively design the graphical
  38. user interface (GUI). Scene Builder generates FXML markup that can be
  39. ported to an IDE where a developer can add the business logic.
  40. - WebView. A web component that uses WebKitHTML technology to make it possible
  41. to embed web pages within a JavaFX application. JavaScript running in
  42. WebView can call Java APIs, and Java APIs can call JavaScript running in
  43. WebView. Support for additional HTML5 features, including Web Sockets, Web
  44. Workers, and Web Fonts, and printing capabilities have been added in JavaFX
  45. 8. See Adding HTML Content to JavaFX Applications.
  46. - Swing interoperabilitiy. Existing Swing applications can be updated with
  47. JavaFX features, such as rich graphis media playback and embedded Web
  48. content. The SwingNode class, which enables you to embed Swing content
  49. into JavaFX applications, has been added in JavaFX 8. See the SwingNode
  50. API javadoc and Embedding Swing Content in JavaFX Applications for
  51. more information.
  52. - Built-in UI controls and CSS. JavaFX provides all the major UI controls that
  53. are required to develop a full-featured application. Components can be
  54. skinned with standard Web technologies such as CSS. The DatePicker and
  55. TreeTableView UI controls are now available with the JavaFX 8 release. See
  56. Using JavaFX UI Controls for more information. Also, the CSS Styleable*
  57. classes have become public API, allowing objects to be styled by CSS.
  58. - Modena theme. The Modena theme replaces the Caspian theme as the default for
  59. JavaFX 8 applications. The Caspian theme is still available for your use by adding the setUserAgentStylesheet(STYLESHEET_CASPIAN) line in your Application start() method. For more information, see the Modena blog at
  60. fxexperience.com
  61. - 3D Graphics Features. The new API classes for Shape3D(Box, Cylinder,
  62. MeshView, and Sphere subclasses), SubScene, Material, PickResult,
  63. LightBase (AmbientLight and PointLight subclasses), and SceneAntialiasing
  64. have been added to the 3D Graphis library in JavaFX 8. The Camera API class
  65. has also been updated in this release. For more information, see the
  66. Getting Started with JavaFX 3D Graphics document and the corresponding
  67. API javadoc for a bunch of shit i am NOT typing out.
  68. - Canvas API. The Canvas API enables drawing directly within an area of the
  69. JavaFX scene that consists of one graphical element (node).
  70. - Printing API. The javafx.print package has been added in Java SE 8 release
  71. and provides the public classes for the JavaFX Printing API
  72. - Rich Text Support. JavaFX 8 brings enhanced text support to JavaFX, including
  73. bi-directional text and complex text scripts, such as Thai and Hindu in
  74. controls, and multi-line, multi-style text in text nodes
  75. - Multitouch Support. JavaFX provides support for multitouch operations, based
  76. on the capabilities of the underlying platform
  77. - Hi-DPI support. JavaFX 8 now supports Hi-DPI displays.
  78. - Hardware-accelerated graphics pipeline. JavaFX graphics are based on the
  79. graphics rendering pipeline (Prism). JavaFX offers smooth graphics that
  80. render quickly through Prism when it is used with a supported graphics
  81. card or graphics processing unit (GPU). If a system does not feature one of
  82. the recommended GPUs supported by JavaFX, then Prism defaults to the
  83. software rendering stack.
  84. - High-performance media engine. The media pipeline supports the playback of
  85. web multimedia content. It provides a stable, low-latency media framework
  86. that is based on the GStreamer multimedia framework.
  87. - Self-contained application deployment model. Self-contained application
  88. packages have all of the application resources and a private copy of the
  89. Java and JavaFX runtimes. They are distributed as native installable
  90. packages and provide the same installation and launch experience as
  91. native applications for that operating system.
  92.  
  93. WHAT CAN I BUILD WITH JAVAFX?
  94.  
  95. With JavaFX, you can build many types of applications. Typically, they are network-aware applications that are deployed across multiple platforms and display information in a high-performance modern user interface that features audio, video, graphics, and animation.
  96.  
  97. okay i skipped a bunch of shit because they really messed up on this tutorial lol
  98.  
  99. 3 HELLO WORLD, JAVAFX STYLE
  100.  
  101. The best way (after two huge chapters) to teach you what it is like to create and build a JavaFX application is with a "Hello World" application. An added benefit of this tutorial is that it enables you to test that your JavaFX technology is properly installed.
  102. The tool used in this tutorial is NetBeans IDE 7.4. Before you begin, ensure that the version of NetBeans IDE that you are using supports JavaFX 8. yeah fuck that
  103. Here are the important things to know about the basic structure of a JavaFX application:
  104.  
  105. - The main class for a JavaFX application extends the
  106. javafx.application.Application class. The start() method is the main entry
  107. point for all JavaFX applications.
  108. - A JavaFX application defines the user interface container by means of a stage
  109. and a scene. The JavaFX Stage class is the top-level JavaFX
  110. container. The JavaFX Scene class is the container for all content. Example
  111. 1-1 creates the stage and scene and makes the scene visible in a given
  112. pixel size.
  113. - In JavaFX, the content of the scene is represented as a hierarchical scene
  114. graph of nodes. In this example, the root node is a StackPane object, which
  115. is a resizable layout node. This means that the root node's size tracks the
  116. scene's size and changes when the stage is resized by a user.
  117. - The root node contains one child node, a button control with text, plus an
  118. event handler to print a message when the button is pressed.
  119. - The main() method is not required for JavaFX applications when the JAR file
  120. for the application is created with the JavaFX packager tool, which embeds
  121. the JavaFX Launcher in the JAR file. However, it is useful to include the
  122. main() method so you can run JAR files that were created without the JavaFX
  123. Launcher, such as when using an IDE in which the JavaFX tools are not fully
  124. integrated. Also, Swing applications that embed JavaFX code require the
  125. main() method.
  126.  
  127. 2 (lol) CREATING A FORM IN JAVAFX
  128.  
  129. Creating a form is a common activity when developing an application. This tutorial teaches you the basics of screen layout, how to add controls to a layout pane, and how to create input events.
  130. In this tutorial, you will use JavaFX to build a login form.
  131.  
  132. CREATE A GRIDPANE LAYOUT
  133.  
  134. For the login form, use a GridPane layout because it enables you to create a flexible grid of rows and columns in which to lay out controls. You can place controls in any cell in the grid, and you can make control span cells as needed.
  135. The code to create the GridPane layout: Add the code before the line primaryStage.show();
  136.  
  137. GridPane grid = new GridPane();
  138. grid.setAlignment(Pos.CENTER);
  139. grid.setHgap(10);
  140. grid.setVgap(10);
  141. grid.setPadding(new Insets(25, 25, 25, 25));
  142.  
  143. Scene scene = new Scene(grid, 300, 275);
  144. primaryStage.setScene(scene);
  145.  
  146. This example creates a GridPane object and assigns it to the variable named grid. The alignment property changes the default position of the grid from the top left of the scene to the center. The gap properties manage the spacing between the rows and columns, while the padding property manages the space around the edges of the grid pane. The insets are in the order of top, right, bottom, and left. In this example there are 25 pixels of padding on each side.
  147. The scene is created with the grid pane as the root node, which is a common practice when working with layout containers. Thus, as the window is resized, the nodes within the grid pane are resized according to their layout constraints. In this example, the grid pane remains in the center when you grow or shrink the window. The padding properties ensure there is a padding around the grid pane when you make the window smaller.
  148. This code sets the scene width and height to 300 by 275. If you do not set the scene dimensions, the scene defaults to the minimum size needed to display its contents.
  149.  
  150. ADD TEXT, LABELS, AND TEXT FIELDS
  151.  
  152. Looking at the previous example, you can see that the form requires the title "Welcome" and text and password fields for gathering information from the user. The code for creating these controls is in the following example. Add this code after the line that sets the grid padding property.
  153.  
  154. Text scenetitle = new Text("Welcome");
  155. scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
  156. grid.add(scenetitle, 0, 0, 2, 1);
  157.  
  158. Label userName = new Label("User Name:");
  159. grid.add(userName, 0, 1);
  160.  
  161. TextField userTextField = new TextField();
  162. grid.add(userTextField, 1, 1);
  163.  
  164. Label pw = new Label("Password:");
  165. grid.add(pw, 0, 2);
  166.  
  167. PasswordField pwBox = new PasswordField();
  168. grid.add(pwBox, 1, 2);
  169.  
  170. The first line creates a Text object that cannot be edited, sets the text to Welcome, and assigns it to a variable named scenetitle. The next line uses the setFont() method to set the font family, weight, and size of the scenetitle variable. Using an inline style is appropriate where the style is bound to a variable, but a better technique for styling the elements of your user interface is by using a style sheet. In the next tutorial, Fancy Forms with JavaFX CSS, you will replace the inline style with a style sheet.
  171. The grid.add() method adds the scenetitle variable to the layout grid. The numbering for columns and rows in the grid starts at zero, and the scenetitle is added in commlumn 0, row 0. The last two arguments of the grid.add() method set the column span to 2 and the row span to 1.
  172. The next lines create a Label object with text User Name at column 0, row 1, and a Text Field object that can be edited. The text field is added to the grid pane at column 1, row 1. A password field and label are created and added to the grid pane in a similar fashion.
  173. When working with the grid pane, you can display the grid lines, which is useful for debugging purposes. In this case, you can add grid.setGridLinesVisible(true) after the line that adds the password field. Then, when you run the application, you see the lines for the grid columns and rows as well as the gap properties, as shown in Figure 2-2.
  174.  
  175. ADD A BUTTON AND TEXT
  176.  
  177. The final two controls required for the application are a Button control for submitting the data and a Text control for displaying a message when the user presses the button.
  178. First, create the button and position it on the bottom right, which is a common placement for buttons that perform an action affecting the entire form. The code is in Example 2-4. Add this code before the code for the scene.
  179.  
  180. Example 2-4 Button
  181.  
  182. Button btn = new Button("Sign in");
  183. HBox hbBtn = new HBox(10);
  184. hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
  185. hbBtn.getChildren().add(btn);
  186. grid.add(hbBtn, 1, 4);
  187.  
  188. The first line creates a button named btn with the label Sign in, and the second line creates an HBox layout pane named hbBtn with spacing of 10 pixels. The HBox pane sets an alignment for the button that is different from the alignment applied to the other controls in the grid pane. The alignment property has a value of Pos.BOTTOM_RIGHT, which positions a node at the bottom of the HBox pane, and the HBox pane is added to the grid in column 1, row 4.
  189. Now, add a Text control for displalying the message, as shown in Example 2-5. Add this code before the code for the scene.
  190.  
  191. ADD CODE TO HANDLE AN EVENT
  192.  
  193. Finally, make the button display the text message when the user presses it. Add the code in Example 2-6 before the code for the scene.
  194.  
  195. btn.setOnAction(new EventHandler<ActionEvent>() {
  196.  
  197. @Override
  198. public void handle(ActionEvent e) {
  199. actiontarget.setFill(Color.FIREBRICK);
  200. actiontarget.setText("Sign in button pressed");
  201. }
  202. });
  203.  
  204. The setOnAction() method is used to register an event handler that sets the actionTarget object to Sign in button pressed when the user presses the button. The color of the actionTarget object is set to firebrick red.
  205.  
  206. RUN THE APPLICATION
  207.  
  208. Right-click the Login project node in the Projects window, choose Run, and then click the Sign in button. If you run into problems, then take a look at the code in the Login.java file.
  209.  
  210. 6 USING FXML TO CREATE A USER INTERFACE
  211.  
  212. This tutorial shows the benefits of using JavaFX FXML, which is an XML-based language that provides the structure for building a user interface separate fromm the application logic of your code.
  213. If you started this document from the beginning, then you have seen how to create a login application using just JavaFX. Here, you use FXML to create the same login user interface, separating the application design from the application logic, thereby making the code easier to maintain. The login user interface you build in this tutorial is shown in Figure 6-1.
  214. A good practice is to set the height and width of the scene when you create it, in this case 300 by 275; otherwise the scene defaults to the minimum size needed to display its contents.
  215.  
  216. MODIFY THE IMPORT STATEMENTS
  217.  
  218. Next, edit the fxml_example.fxml file. This file specifies the user interface that is displayed when the application starts. The first task is to modify the import statements so your code looks like Example 6-2.
  219. As in Java, class names can be fully qualified (including the package name), or they can be imported using the import statement, as shown in Example 6-2. If you prefer, you can use specific import statements that refer to classes.
  220.  
  221. CREATE A GRIDPANE LAYOUT
  222.  
  223. The Hello World application generated by NetBeans uses an AnchorPane layout. For the login form, you will use a GridPane layout because it enables you to create a flexible grid of rows and columns in which to lay out controls.
  224. Remove the AnchorPane layout and its children and replace it with the GridPane layout in Example 6-3.
  225.  
  226. <GridPane fx:controller="fxmlexample.FXMLExampleController"
  227. xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
  228. <padding><Insets top="25" right="25" bottom="10" left="25"/></padding>
  229.  
  230. </GridPane>
  231.  
  232. In this application, the GridPane layout is the root element of the FXML document and as such has two attributes. The fx:controller attribute is required when you specify controller-based event handlers in your markup. The xmlns:fx attribute is always required and specifies the fx namespace.
  233. The remainder of the code controls the alignment and spacing of the grid pane. The alignment property changes the default position of the grid from the top left of the scene to the center. The gap properties manage the spacing between the rows and columns, while the padding property manages the space around the edges of the grid pane.
  234. As the window is resized, the nodes within the grid pane are resized according to their layout constraints. In this example, the grid remains in the center when you grow or shrink the window. The padding properties ensure there is a padding around the grid when you make the window smaller.
  235.  
  236. ADD TEXT AND PASSWORD FIELDS
  237.  
  238. Looking back at Figure 6-1, you can see that the login form requires the title "Welcome" and text and password fields for gathering information from the user. The code in Example 6-4 is part of the GridPane layout and must be placed above the </GridPane> statment.
  239.  
  240. <Text text="Welcome"
  241. GridPane.columnIndex="0" GridPane.rowIndex="0"
  242. GridPane.columnSpan="2"/>
  243.  
  244. <Label text="User Name:"
  245. GridPane.columnIndex="0" GridPane.rowIndex="1"/>
  246.  
  247. <TextField
  248. GridPane.columnIndex="1" GridPane.rowIndex="1"/>
  249.  
  250. <Label text="Password:"
  251. GridPane.columnIndex="0" GridPane.rowIndex="2"/>
  252.  
  253. <PasswordField fx:id="passwordField"
  254. GridPane.columnIndex="1" GridPane.rowIndex="2"/>
  255.  
  256. The first line creates a Text object and sets its text value to Welcome. The GridPane.columnIndex and GridPane.rowIndex attributes correspond to the placement of the Text control in the grid. The numbering for rows and columns in the grid starts at zero, and the location of the Text control is set to 0,0, meaning it is in the first column of the first row. The GridPane.columnSpan attribute is sset to 2, making the Welcome title span two columns in the grid. You will need this extra width later in the tutorial when you add a style sheet to increase the font size of the text to 32 points.
  257. The next lines create a Label object with text User Name at column 0, row 1 and a TextField object to the right of it at column 1, row 1. Another Label and PasswordField object are created and added to the grid in a similar fashion.
  258. When working with a grid layout, you can display the grid lines, which is useful for debugging purposes. In this case, set the gridLinesVisible property to true by adding the statement <gridLinesVisible>true</gridLinesVisible> right after the <padding></padding> statement. Then, when you run the application, you see the lines for the grid columns and rows as well as the gap properties, as shown in Figure 6-2.
  259.  
  260. ADD A BUTTON AND TEXT
  261.  
  262. The final two controls required for the application are a button control for submitting the data and a Text control for displaying a message when the user presses the button. The code is in Example 6-5. Add this code before </GridPane>.
  263.  
  264. <HBox spacing="10" alignment="bottom_right"
  265. GridPane.columnIndex="1" GridPane.rowIndex="4">
  266. <Button text="Sign In"
  267. onAction="#handleSubmitButtonAction"/>
  268. </HBox>
  269.  
  270. <Text fx:id="actiontarget"
  271. GridPane.columnIndex="0" GridPane.columnSpan="2"
  272. GridPane.halignment="RIGHT" GridPane.rowIndex="6"/>
  273.  
  274. An HBox pane is needed to set an alignment for the button that is different from the default alignment applied to the other controls in the GridPane layout. The alignment property is set to bottom_right, which positions a node at the bottom of the space vertically and at the right edge of the space horizontally. The HBox pane is added to the grid in column 1, row 4.
  275. The HBox pane has one child, a Button with text property set to Sign in and an onAction property set to handleSubmitButtonAction(). While FXML is a convenient way to define the structure of an application's user interface, it does not provide a way to implement an application's behavior. You implement the behavior for the handleSubmitButtonAction() method in Java code in the next section of this tutorial, Add Code to Handle an Event.
  276. Assigning an fx:id value to an element, as shown in the code for the Text control, creates a variable in the document's namespace, which you can refer to from elsewhere in the code. While not required, defining a controller field helps clarify how the controller and markup are asociated.
  277.  
  278. ADD CODE TO HANDLE AN EVENT
  279.  
  280. Now make the Text control display a message when the user presses the button. You do this in the FXMLExampleController.java file. Delete the code lol blah blah.
  281. The @FXML annotation is used to tag nonpublic controller member fields and handler methods for use by FXML markup. The handleSubmitButtonAction method sets the actiontarget variable to Sign in button pressed when the user presses the button.
  282. You can run the application now to see the complete user interface. Figure 6-3 shows the results when you type text in the two fields and click the Sign in button. If you have any problems, then you can compare your code against the FXMLLogin example.
  283.  
  284. USE A SCRIPTING LANGUAGE TO HANDLE EVENTS
  285.  
  286. As an alternative to using Java code to create an event handler, you can create the handler with any language that provides a JSR 223-compatible scripting engine. Such languages include JavaScript, Groovy, Jython, and Clojure.
  287. Optionally, you can try using JavaScript now.
  288. skipped some shit
  289. Alternatively, you can put the JavaScript functions in an external file (such as fxml_example.js) and include the script like this:
  290.  
  291. <fx:script source="fxml_example.js"/>
  292.  
  293. STYLE THE APPLICATION WITH CSS
  294.  
  295. The final task is to make the login application look attractive by adding a Cascading Style Sheet (CSS).
  296.  
  297. <stylesheets>
  298. <URL value="@Login.css" />
  299. </stylesheets>
  300.  
  301. </GridPane>
  302.  
  303. The @ symbol before the name of the style sheet in the URL indicates that the style sheet is in the same diretory as the FXML file.
  304.  
  305.  
  306. and so they just dump you into another tutorial
  307.  
  308. OVERVIEW
  309.  
  310. FXML is a scriptable, XML-based markup language for constructing Java object graphs. It provides a convenient alternative to constructing such graphs in procedural code, and is ideally suited to defining the user interface of a JavaFX application, since the hierarchical structure of an XML document closely parallels the structure of the JavaFX scene graph.
  311. This document introduces the FXML markup language and explains how it can be used to simplify development of JavaFX applications.
  312.  
  313. ELEMENTS
  314.  
  315. In FXML, an XML element represents one of the following:
  316.  
  317. - A class instance
  318. - A property of a class instance
  319. - A "static" property
  320. - A "define" block
  321. - A block of script code
  322.  
  323. Class instances, instance properties, static properties, and define blocks are discussed in this section below. Scripting is discussed in a later section.
  324.  
  325. CLASS INSTANCE ELEMENTS
  326.  
  327. Class instances can be constructed in FXML in several ways. The most common is via instance declaration elements, which simply create a new instance of a class by name. Other ways of creating class instances include referencing existing values, copying existing values, and including external FXML files. Each is discussed in more detail below.
  328.  
  329. INSTANCE DECLARATIONS
  330.  
  331. If an element's tag is considered an instance declaration if the tag begins with an uppercase letter (and the class is imported) or, as in Java, it denotes a fully-qualified (including the package name) name of a class. When the FXML loader (also introduced later) encounters such an element, it creates an instance of that class.
  332. Importing a class is done using the "import" processing instruction (PI). For example, the following PI imports the javafx.scene.control.Label class into the current FXML document's namespace:
  333.  
  334. <?import javafx.scene.control.Label?>
  335.  
  336. This PI imports all classes from the javafx.scene.control package into the current namespace:
  337.  
  338. <?import javafx.scene.control.*?>
  339.  
  340. Any class that adheres to JavaBean constructor and property naming conventions can be readily instantiated and configured using FXML. The following is a simple but complete example that creates an instance of javafx.scene.control.Label and sets its "text" property to "Hello, World!":
  341.  
  342. <?import javafx.scene.control.Label?>
  343. <Label text="Hello, World!"/>
  344.  
  345. Note that the Label's "text" property in this example is set using an XML attribute. Properties can also be set using nested property elements. Property elements are discussed in more detail later in this section. Property attributes are discussed in a later section.
  346. Classes that don't conform to Bean conventions can also be constructed in FXML, using an object called a "builder". Builders are discussed in more detail later.
  347.  
  348. MAPS
  349.  
  350. Internally, the FXML loader uses an instance of com.sun.javafx.fxml.BeanAdapter to wrap an instantiated object and invoke its setter methods. This (currently) private class implements the java.util.Map interface and allows a caller to get and set Bean property values as key/value pairs.
  351. If an element represents a ty pe that already implements Map (such as java.util.HashMap), it is not wrapped and its get() and put() methods are invoked directly. For example, the following FXML creates an instance of HashMap and sets its "foo" and "bar" values to "123" and "456", respectively:
  352.  
  353. <HashMap foo="123" bar="456"/>
  354.  
  355. FX:VALUE
  356.  
  357. The fx:value attribute can be used to initialize an instance of a type that does not have a default constructor but provides a static valueOf(String) method. For example, java.lang.String as well as each of the primitive wrapper types define a valueOf() method and can be constructed in FXML as follows:
  358.  
  359. <String fx:value="Hello, World!"/>
  360. <Double fx:value="1.0"/>
  361. <Boolean fx:value="false"/>
  362.  
  363. Custom classes that define a static valueOf(String) method can also be constructed this way.
  364.  
  365. FX:FACTORY
  366.  
  367. The fx:factory attribute is another means of creating objects whose classes do not have a default constructor. The value of the attribute is the name of a static, no-arg factory method for producing class instances. For example, the following markup creates an instance of an observable array list, populated with three string values:
  368.  
  369. <FXCollections fx:factory="observableArrayList">
  370. <String fx:value="A"/>
  371. <String fx:value="B"/>
  372. <String fx:value="C"/>
  373. </FXCollections>
  374.  
  375. BUILDERS
  376.  
  377. A third means of creating instances of classes that do not conform to Bean conventions (such as those representing immutable values) is a "builder". The builder design pattern delegates object construction to a mutable helper class (called a "builder") that is responsible for manufacturing instances of the immutable type.
  378. Builder support in FXML is provided by two interfaces. The javafx.util.Builder interface defines a single method named build() which is responsible for constructing the actual object:
  379.  
  380. public interface Builer<T> {
  381. public T build();
  382. }
  383.  
  384. A javafx.util.BuilderFactory is responsible for producing builders that are capable of instantiating a given type:
  385.  
  386. public interface BuilderFactory {
  387. public Builder<?> getBuilder(Class<?> type);
  388. }
  389.  
  390. A default builder factory, JavaFXBuilderFactory, is provided in the javafx.fxml package. This factory is capable of creating and configuring more immutable JavaFX types. For example, the following markup uses the default builder to create an instance of the immutable javafx.scene.paint.Color class:
  391.  
  392. <Color red="1.0" green="0.0" blue="0.0"/>
  393.  
  394. Note that, unlike Bean types, which are constructed when the element's start tag is processed, objects constructed by a builder are not instantiated until the element's closing tag is reached. This is because all of the required arguments may not be available until the element has been fully processed. For example, the Color object in the preceding example could also be written as:
  395.  
  396. <Color>
  397. <red>1.0</red>
  398. <green>0.0</green>
  399. <blue>0.0</blue>
  400. </Color>
  401.  
  402. The Color instance cannot be fully construced until all three of the color components are known.
  403. When processing markup for an object that will be constructed by a builder, the Builder instances are treated like value objects - if a Builder implements the Map interface, the put() method is used to set the builders attribute values. Otherwise, the builer is wrapped in a BeanAdapter and its properties are assumed to be exposed via standard Bean setters.
  404.  
  405. FX:INCLUDE
  406.  
  407. The <fx:include> tag creates an object from FXML markup defined in another file. It is used as follows:
  408.  
  409. <fx:include source="filenamme"/>
  410.  
  411. where filename is the name of the FXML file to include. Values that begin with a leading slash character are treated as relative to the classpath. Values with no leading slash are considered relative to the path of the current document.
  412. For example, given the following markup:
  413.  
  414. <?import javafx.scene.control.*?>
  415. <?import javafx.scene.layout.*?>
  416. <VBox xmlns:fx="http://javafx.com/fxml">
  417. <children>
  418. <fx:include source="my_button.fxml"/>
  419. </children>
  420. </VBox>
  421.  
  422. If my_button.fxml contains the following:
  423.  
  424. <?import javafx.scene.control.*?>
  425. <Button text="My Button"/>
  426.  
  427. the resulting scene graph would contain a VBox as a root object with a single Button as a child node.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement