FXML of javafx

Source: Internet
Author: User

FXML is used as the XML-based UI constructor. Its Related rules are worth understanding.

FXML element classification:

    A class instanceA property of a class instanceA "static" propertyA "define" blockA block of script codeFXML should be defined in the root element prefix: xmlns: fx = http://javafx.com/xmlClass instance element instance statement
      
    Maps
      

    Fx: value-for classes without Default constructors, such as String and Double, but with the valueOf Method
      
      
      

    Fx: factory-for static factory methods
          
           
           
       
      

    Builders uses the constructor mode class: such as Color
      
          
       
        1.0
           
       
        0.0
           
       
        0.0
       
      

    Fx: include-contains another fxml file or international resource file resource bundle
      
      
      
          
               
            
       
      
    My_button.fxml
      
    Contains international resource files
      

    Fx: constant
        
       
        
       

    Fx: reference-reference using fx: id
          
               
       // Used to replace the image attribute of ImageView
      
        
          
          
      

    Fx: copy-Temporarily unavailable, may change later
    Fx: root-pointing to the root element
    The Property element supports forced type conversion. Divided:
      A property setpolicread-only list propertyA read-only map propertyProperty Setters
             
          
           Hello, World!
          
         

      ReadOnly List Property
             
                  
                   ...    
          
         

      ReadOnly Map Property
             
          

      Default Property
         
         
             
              ...
         

      Static Property
             
                  
                       
            
             0
                   
            
             0
                        
          
         

      Defining the content defined by Blocks ---- fx: definefx: define is not added to the Scene Graph. The most typical application is to add the symbol $
             
                  
               
              
                  
                   
                   
               
          
         



      Attributes: Category:
        A property of a class instanceA "static" propertyAn event handlerProperty Attribute differs from Property Element: 1. property attribute takes effect only when the element is disabled. property attribute also supports resolution operators ):
          Location resolution Resource resolution international Resource resolution Variable resolution Location resolution @ indicates that the current fxml file is in the same directory
                   
                        
                    
               
          Note that @ path resolution must be followed by URL-encoded characters. For example, My Image.jpg should be written as this.
           
              
          Resource resolution % indicates that the variable should be parsed using international resources
           
              
          Variable resolution $ indicates Variable parsing, which is generally used in combination with fx: define.
                   
                
               ...
               
               

          Escape processing:
           
              

          Expression binding: $ {expr}
               

          Other operations supported:
          "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 is similar to Instance Properties.
          Static properties attribute differs from element:
            

          EventHandlers applies to setOnEvent class methods (such as setOnAction)
          Scripting: javascript declaration and script
               ...
                   
                        
                     
                
               

          Controller-like Controller Method Processing Method: Note #, with @ FXML Annotation
                   
                        
                     
                
               
          public class MyController {   @FXML public void handleButtonAction(ActionEvent event) {        System.out.println("You clicked me!");    }}
          This method is also valid.
          public class MyController {    public void handleButtonAction() {        System.out.println("You clicked me!");    }}

          For special handling of Collections and properties, ObservableMap orObservableSet uses a special onChange attribute that points to a handler method with counter. Change, MapChangeListener. Change or SetChangeListener. Changeparameter respectively.
                   
                
               
          public class MyController {    public void handleChildrenChange(ListChangeListener.Change c) {        System.out.println("Children changed!");    }}

          Processing of parent property:
          public class MyController {    public void handleParentChange(ObservableValue value, Parent oldValue, Parent newValue) {        System.out.println("Parent changed!");    }}
               


          Scripting
                
                
                
                    
                 
                      function handleButtonAction(event) {       java.lang.System.out.println('You clicked me!');    }    
                     
                         
                      
                 
                

          Read scripts from external files.
                
                
                    
                     
                         
                      
                 
                
          Example. js:
          function handleButtonAction(event) {   java.lang.System.out.println('You clicked me!');}
                
                 var myText = "This is the text of my label.";
                ...

          Controllersfx: controller
                    
                         
                      
                 
                
          public class MyController {    public void handleButtonAction(ActionEvent event) {        System.out.println("You clicked me!");    }}
          Of course, the Controllers class can also implement the Initializable interface so that the initialization method initialize () can be called during loading ()
                    
                         
                      
                 
                
          package com.foo;public class MyController implements Initializable {    public Button button;    @Override    public void initialize(URL location, Resources resources)        button.setOnAction(new EventHandler() {            @Override            public void handle(ActionEvent event) {                System.out.println("You clicked me!");            }
          The fields and methods are modified through public, but this destroys the encapsulation principle. Since the controller is visible to FXML Loader, there is no need to open access to the outside. In this way, the @ FXML annotation must be used for modification.
          public class MyController {    @FXML    private void handleButtonAction(ActionEvent event) {        System.out.println("You clicked me!");    }}
          public class MyController implements Initializable {    @FXML private Button button;    @FXML    protected void initialize()        button.setOnAction(new EventHandler() {            @Override            public void handle(ActionEvent event) {                System.out.println("You clicked me!");            }

          Access by the Nested controller of Nested Controllers.
          FXMLLoader
          URL location = getClass().getResource("example.fxml");ResourceBundle resources = ResourceBundle.getBundle("com.foo.example");FXMLLoader fxmlLoader = new FXMLLoader(location, resources);Pane root = (Pane)fxmlLoader.load();MyController controller = (MyController)fxmlLoader.getController();

          Use FXML in combination with FXMLLoader to customize the UI component by using The element specifies the type of the root element:
                 
                 
                 
                     
                      
                  
                 
          Custom UI components:
          package fxml;import java.io.IOException;import javafx.beans.property.StringProperty;import javafx.fxml.FXML;import javafx.fxml.FXMLLoader;import javafx.scene.control.TextField;import javafx.scene.layout.VBox;public class CustomControl extends VBox {    @FXML private TextField textField;    public CustomControl() {        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("custom_control.fxml"));        fxmlLoader.setRoot(this);        fxmlLoader.setController(this);        try {            fxmlLoader.load();        } 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 textField.textProperty();    }    @FXML    protected void doSomething() {        System.out.println("The button was clicked!");    }}

          Use custom UI components:
          HBox hbox = new HBox();CustomControl customControl = new CustomControl();customControl.setText("Hello World!");hbox.getChildren().add(customControl);

                     
                  
                 





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.