JavaFX FXMLLoader class

Description

JavaFX FXMLLoader class

import java.io.IOException;
import java.net.URL;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);//  ww  w .j a  v a 2 s .  c  om
  }

  @Override
  public void start(Stage stage) throws IOException {
    // Construct a URL for the FXML document
    URL fxmlUrl = this.getClass().getResource("/resources/fxml/hello.fxml");

    // Load the FXML document
    VBox root = FXMLLoader.<VBox>load(fxmlUrl);
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.setTitle("Hello FXML");
    stage.show();
  }
}

The Contents of the hello.fxml File

<?xml version="1.0" encoding="UTF-8"?> 
<?language javascript?> //w w  w  . j a v a2s  . c o  m
<?import javafx.scene.Scene?> 
<?import javafx.scene.layout.VBox?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.Button?> 

<VBox spacing="10" xmlns:fx="http://javafx.com/fxml">     
    <Label fx:id="msgLbl" text="FXML is cool!" prefWidth="150"/> 
    <Button fx:id="sayHelloBtn" text="Say Hello" onAction="sayHello()"/> 
        <style> 
        -fx-padding: 10; 
        -fx-border-style: solid inside; 
        -fx-border-width: 2; 
        -fx-border-insets: 5; 
        -fx-border-radius: 5; 
        -fx-border-color: blue; 
    </style>     
    <fx:script>         
        function sayHello() { 
            msgLbl.setText("Hello from FXML!"); 
        } 
    </fx:script>     
</VBox> 



PreviousNext

Related