web123456

A preliminary study on using java, javafx in fxml

FXML is an XML-based, UI constructor. The relevant rules are worth understanding.

FXML element classification:

A class instance

A property of a class instance

A "static" property

A "define" block

A block of script code

FXML should define prefix in the root element: xmlns:fx=http://javafx.com/xml

Class instance element

Example statement

Maps

fx:value-For classes without default constructors, such asString, Double etc. but with valueOf method

fx:factory-For use of static factory methods

Builders use constructor patterns classes: such as Color

1.0

0.0

0.0

fx:include-Contains another fxml file or international resource file resource bundle

my_button.fxml

Includes international resource files

fx:constant

fx:reference-referenced by fx:id

// Used to replace ImageViewimageproperty

fx:copy-Don't use it for the time being, it may change in the future

fx:root-points to root element

Property elements

The property element supports casting.

Classified into:

A property setter

A read-only list property

A read-only map property

Property Setters

Hello, World!

ReadOnly List Property

fill="#ff0000"/>

...

ReadOnly Map Property

Default Property

...

Static Property

0

0

Define Blocks----fx:define

The content defined by fx:define will not be added to Scene Graph. The most typical application is fx:define of ToggleGroup in the radio button group

You need to add the symbol $ before referring to it

Attributes:

Classification:

A property of a class instance

A "static" property

An event handler

Property Attribute and PropertyElementThere is a difference:

attribute will only take effect when the element is closed

The attribute also supports resolution operators:

Location resolution location resolution

Resource resolution international resource analysis

Variable resolution variable analysis

Location resolution

@ means in the same directory as the current fxml file

Note that the following @ path parsing must be characters that have been URL-encoded. If My should be written like this

Resource resolution

% means that the character variable should be parsed with international resources

Variable resolution

$ means variable parsing is required, and is generally used in conjunction with fx:define

...

Escape processing:

Expression binding:

${expr}

Other supported operations:

"string"

‘string‘

A string constant

true

false

A boolean constant

null

A constant representing the null value

50.0

3e5

42

A numerical constant

-

(unary operator)

Unary minus operator, applied on a number

!

(unary operator)

Unary negation of a boolean

+ -

* / %

Numerical binary operators

&& ||

Boolean binary operators

> >=

< <=

== !=

Binary operators of comparison.

Both arguments must be of type Comparable

Static Properties are similar to Instance Properties

The static properties attribute is somewhat different from element:

EventHandlers

Methods suitable for setOnEvent class (such as setOnAction)

Scripted processing method:

usejavascriptDeclarations and scripts

...

onAction="(‘You clicked me!‘);"/>

Controller method processing method:

Note the # number, annotated with @FXML

xmlns:fx="/fxml">

public class MyController {

@FXML public void handleButtonAction(ActionEvent event) {

("You clicked me!");

}

}

The following method is also effective

public class MyController {

public void handleButtonAction() {

("You clicked me!");

}

}

Special handling of Collections and properties

ObservableList,ObservableMaporObservableSetuses

a specialonChangeattribute that points

to a handler method with ,

respectively.

xmlns:fx="/fxml">

public class MyController {

public void handleChildrenChange( c) {

("Children changed!");

}

}

For parent property processing:

public class MyController {

public void handleParentChange(ObservableValue value, Parent oldValue, Parent newValue) {

("Parent changed!");

}

}

xmlns:fx="/fxml" onParentChange="#handleParentChange"/>

Scripting

function handleButtonAction(event) {

(‘You clicked me!‘);

}

Read scripts from external files.

:

function handleButtonAction(event) {

(‘You clicked me!‘);

}

var myText = "This is the text of my label.";

...

Controllers

fx:controller

xmlns:fx="/fxml">

public class MyController {

public void handleButtonAction(ActionEvent event) {

("You clicked me!");

}

}

Of course, the Controllers class can also implement the Initializable interface, so that the initialization method initialize() can be called when loaded

xmlns:fx="/fxml">

package ;

public class MyController implements Initializable {

public Button button;

@Override

public void initialize(URL location, Resources resources)

(new EventHandler() {

@Override

public void handle(ActionEvent event) {

("You clicked me!");

}

The above mentioned are all fields and methods that are modified through public, but this destroys the encapsulation principle.

Since the controller is visible to the FXML Loader, there is no need to open access to the outside.

In this way, we must modify it through @FXML annotation.

public class MyController {

@FXML

private void handleButtonAction(ActionEvent event) {

("You clicked me!");

}

}

public class MyController implements Initializable {

@FXML private Button button;

@FXML

protected void initialize()

(new EventHandler() {

@Override

public void handle(ActionEvent event) {

("You clicked me!");

}

Nested Controllers nested controller access.

FXMLLoader

URL location = getClass().getResource("");

ResourceBundle resources = ("");

FXMLLoader fxmlLoader = new FXMLLoader(location, resources);

Pane root = (Pane)();

MyController controller = (MyController)();

Use FXML in conjunction with FXMLLoader to customize UIComponents

Specify the type of the root element by using the element:

Custom UI components:

package fxml;

import ;

import ;

import ;

import ;

import ;

import ;

public class CustomControl extends VBox {

@FXML private TextField textField;

public CustomControl() {

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("custom_control.fxml"));

(this);

(this);

try {

();

} catch (IOException exception) {

throw new RuntimeException(exception);

}

}

public String getText() {

return textProperty().get();

}

public void setText(String value) {

textProperty().set(value);

}

public StringProperty textProperty() {

return ();

}

@FXML

protected void doSomething() {

("The button was clicked!");

}

}

Use custom UI components:

HBox hbox = new HBox();

CustomControl customControl = new CustomControl();

("Hello World!");

().add(customControl);