Quick Contact

    Hibernate

    JavaFX Controller

    JavaFX controller works based on MVC(Model-View-Controller) JavaFX MVC can be achieved by FXML (EFF-ectseXtended Markup Language). FXML is an XML based language used to develop the graphical user interfaces for JavaFX applications as in the HTML. FXML can be used to build an entire GUI application scene or part of a GUI application scene. This FXML allows developers for separate User Interface logic from the business logic. If suppose User Interface in your JavaFX application, then no need to compile the application even if we have done some changes to the application. If we want, we can edit the FXML in the editor and re-run the app.

    Real-Time Example:

    Whenever a user wants to change a specific part of the standalone application, courses portion an application. We know that always new courses come to the market so, every time we can re-compile the code. So, in that case, we can use FXML in the controller class.

    What is the FXML Controller?

    Here, we discuss the FXML controller in JavaFX with an explanation.

    Syntax:

    < pre>
    < ?xml version="1.0" encoding="UTF-8"?>


    < HBox>
    < children>
    < Label text="Hello, Well Come to EDUCBA Online Course"/>
    < /children>
    < /HBox>

    Explanation:

    • You can observe that all components are specified just like HTML
    • < HBox> creates horizontal component.
    • < children> tag specifies inside components will be added as children to the above
    • , importing HBox and Label classes.

    1. 1. Importing Classes in FXML Controller

      In FXML controller we must import classes as below:

      Syntax:

    2. 2. Loading FXML file in Controller

      Loading FXML file in controller we must import javafx.fxml.FXMLLoader package.

      Syntax:

      FXMLLoaderfxmlLoader = new FXMLLoader();//creating FXMLLoader object

      FXMLLoaderfxmlLoader = new FXMLLoader();//creating FXMLLoader object
      fxmlLoader.setLocation(new URL(“path/fileName.fxml”));//accessing FXML file

      How Does FXML Controller Work in JavaFX?

      We can set the controller class for the FXML document. The FXML controller class can bind the Graphical User Interface components declared within the FXML file together and also it makes the controller object becomes a mediator. We can set the controller for FXML in 2 ways:

    1. How Does FXML Controller Work in JavaFX?

      We can set the controller class for the FXML document. The FXML controller class can bind the Graphical User Interface components declared within the FXML file together and also it makes the controller object becomes a mediator. We can set the controller for FXML in 2 ways:

      1. Specifying Inside the FXML File Directly

        Following is a syntax to Specifying inside the FXML file directly.

    2. Syntax:

      < ?xml version="1.0" encoding="UTF-8"?>
      < ?import javafx.scene.layout.HBox?>
      < ?import javafx.scene.control.Button?>
      < HBoxxmlns:fx="http://javafx.com/fxml" fx:controller="com.jenkov.javafx.ClassNameOfController" >
      < Button text="Press Me"/ onAction="clickMeAction()">
      < /Button> 
      < /HBox>
      

      Explanation:

      • fx:controller is used to including controller class.
      • Button onAction attribute used for performing clicking action.
    3. Creating Separate FXML Controller Class

      Following is a syntax to create a separate FXML controller class and includes it with the FXML Loader instance to load the FXML file.

      Syntax:

      ClassNameOfControllercontrollerInstance = new ClassNameOfController();
      FXMLLoaderfxmlLoader= new FXMLLoader();
      fxmlLoader.setControllercontrollerInstance(controllerInstance);
      

      We can also include CSS styles and JavaScript in the FXML file inside the component.

      Syntax:

      < style>
      -fx-property: value;//CSS
      < /style>
      < fx:script>
      //JavaScript logic
      < /fx:script>
      

      Examples of JavaFX Controller

      In this tutorial, we will create all the FXML and controller classes separately for better understanding.

      Example #1 – FXML Controller with Label and Field

      FXML Code:

      FXMLLabelTextFieldController.fxml
      
      
      
      < HBox>
      < Label text="First Name" />
      < TextField text="enter your name" />
      < Label text="Last Name" />
      < TextField text="enter your last name" />
      < Label text="Mobile Number" />
      < TextField text="enter your mobile number" />
      < /HBox>
      

      JavaFX Code:

      FXMLLabelTextFieldController.java

      package com.fxml.controller;
      import java.io.FileInputStream;
      import java.io.IOException;
      import javafx.application.Application;
      import javafx.fxml.FXMLLoader;
      import javafx.scene.Scene;
      import javafx.scene.layout.HBox;
      import javafx.stage.Stage;
      public class FXMLController extends Application {
      @Override
      public void start(Stage outStage) throws IOException {
      // Setting title to screen
      outStage.setTitle("FXML Controller Demo");
      // Creating FXML Loader instance
      FXMLLoader loader = new FXMLLoader();
      // FXML path
      String fxmlActualPath = "C://Users//paramesh//Desktop//Desktop//Verinon Purpose//FXMLController//src//com//fxml//controller/FXMLController.fxml";
      // Setting FXML path
      FileInputStreamfxmlStream = new FileInputStream(fxmlActualPath);
      // Creating VBox to add FXML label and text fields
      HBoxhBox = (HBox) loader.load(fxmlStream);
      // Creating scene
      Scene screen = new Scene(hBox);
      // Setting screen stage
      outStage.setScene(screen);
      // Showing the screen
      outStage.show();
      }
      public static void main(String[] args) {
      //inside JVM launch method calls start method
      Application.launch(args);
      }
      }
      

      Example #2 – FXML Controller with Button and Label

      FXML Code:

      FXMLAddingButtonsController.fxml
      < ?import javafx.scene.layout.VBox?>
      < ?import javafx.scene.control.Label?>
      < ?import javafx.scene.control.TextField?>
      < ?import javafx.scene.control.Button?>
      < VBox>
      < Label text="Button 1" />
      < Button text="FirstName Button" />
      < Label text="Button 2" />
      < Button text="LastName Button" />
      < Label text="Button 3" />
      < Button text="MobileNumber Button" />
      < Label text="Button 4" />
      < Button text="Email Button" />
      < Label text="Button 5" />
      < Button text="Login Button" />
      < /VBox>
      

      JavaFX Code:

      FXMLAddingButtonsController.java

      package com.fxml.controller;
      import java.io.FileInputStream;
      import java.io.IOException;
      import javafx.application.Application;
      import javafx.fxml.FXMLLoader;
      import javafx.scene.Scene;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;
      public class FXMLAddingButtonsController extends Application {
      @Override
      public void start(Stage outStage) throws IOException {
      // Setting title to screen
      outStage.setTitle("FXML Controller Button with CSS Styles");
      // Creating FXML Loader instance
      FXMLLoader loader = new FXMLLoader();
      // FXML path
      String fxmlActualPath = "C://Users//paramesh//Desktop//Desktop//Verinon Purpose//FXMLController//src//com//fxml//controller/FXMLLabelAddingButtonController.fxml";
      // Setting FXML path
      FileInputStreamfxmlStream = new FileInputStream(fxmlActualPath);
      // Creating VBox to add FXML label and text fields
      VBoxvBox = (VBox) loader.load(fxmlStream);
      // Creating scene
      Scene screen = new Scene(vBox, 500, 500);
      // Setting screen stage
      outStage.setScene(screen);
      // Showing the screen
      outStage.show();
      }
      public static void main(String[] args) {
      // inside JVM launch method calls start method
      Application.launch(args);
      }
      }
      

      Example #3 – FXML Controller Button Action

      FXML Code:

      FXMLButtonActionController.fxml
      < ?language JavaScript?>
      < ?import javafx.scene.layout.VBox?>
      < ?import javafx.scene.control.Label?>
      < ?import javafx.scene.control.Button?>
      < ?import javafx.scene.control.TextField?>
      < VBoxxmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
      < children>
      < Label text="Name :" />
      < TextFieldfx:id="inputText1" text="enter your name"/>
      < /children>
      < children>
      < Label text="Email :" />
      < TextFieldfx:id="inputText2" text="enter your email"/>
      < /children>
      < children>
      < Button onAction="showDetails()" text="Display Details" />
      < Label fx:id="labelOut" />
      < /children>
      < !-- including JavaScript action -->
      < fx:script>
      function showDetails()
      {
      labelOut.setText("Name=>"+inputText1.getText()+" Email=>"+inputText2.getText());
      }
      < /fx:script>
      < /VBox>
      

    JavaFX Code:

    FXMLButtonActionController.java

    package com.fxml.controller;
    import java.io.FileInputStream;
    import java.io.IOException;
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Scene;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    public class FXMLButtonActionController extends Application {
    public static void main(String[] args) {
    Application.launch(args);
    }
    @Override
    public void start(Stage stage) throws IOException {
    // Create the FXMLLoader
    FXMLLoader loader = new FXMLLoader();
    // Path to the FXML File
    String fxmlDocPath = "C://Users//paramesh//Desktop//Desktop//Verinon Purpose//FXMLController//src//com//fxml//controller/FXMLButtonActionController.fxml";
    FileInputStreamfxmlStream = new FileInputStream(fxmlDocPath);
    // Create the Pane and all Details
    VBox root = (VBox) loader.load(fxmlStream);
    // Create the Scene
    Scenescene = new Scene(root,300,300);
    // Set the Scene to the Stage
    stage.setScene(scene);
    // Set the Title to the Stage
    stage.setTitle("A simple FXML Example");
    // Display the Stage
    stage.show();
    }
    }
    

    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.