Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why member is not injected in JavaFX controller?

I have the following FXML:

<BorderPane fx:controller="mypackage.MainStage" id="BorderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
  <bottom>
    <ToolBar>
      <items>
        <Slider id="slider" prefWidth="443.0" />
        <Label text="Record:" />
        <TextField prefWidth="46.0" />
        <Label text=" of " />
        <Label id="totalLabel" text="0" />
      </items>
    </ToolBar>
  </bottom>
  <center>
    <Pane id="mainPane" prefHeight="200.0" prefWidth="200.0" />
  </center>
  <top>
    <ToolBar>
      <items>
        <Button mnemonicParsing="false" text="Load more" />
      </items>
    </ToolBar>
  </top>
</BorderPane>

and the following controller code:

public class MainStage implements Initializable {

    @FXML
    private Pane mainPane;

    @FXML
    private Label totalLabel;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        totalLabel.setText("245");
    }

}

and the following application code

public class MyClass extends Application
{

    @Override
    public void start(Stage stage) throws Exception {

        Parent root = FXMLLoader.load(getClass().getResource("MainStage.fxml"));

        Scene scene = new Scene(root);

        stage.setTitle("MyClass");
        stage.setScene(scene);
        stage.show();


    }

    public static void main( String[] args )
    {
        launch(args);
    }


}

If I set breakpoint to initialize() I see it is called, but member is null.

Why isn't it injected?

like image 896
Suzan Cioc Avatar asked Nov 20 '13 21:11

Suzan Cioc


People also ask

What is the purpose of the @fxml notation in a JavaFX controller file is it required?

The @FXML annotation is used to tag nonpublic controller member fields and handler methods for use by FXML markup. The handleSubmtButtonAction method sets the actiontarget variable to Sign in button pressed when the user presses the button. You can run the application now to see the complete user interface.

What is the purpose of the controller JavaFX?

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.

What is FXMLLoader in JavaFX?

The FXMLLoader object is a mixed-function class with the responsibility to parse FXML content (as XML), build the Scene Graph and initialise the Controller of a JavaFX View. JavaFX's FXML markup language allows you to separate the business logic of a program from the user interface design.21-Aug-2020.

What does the Init method do in JavaFX?

The init method is called on the launcher thread, not on the JavaFX Application Thread. This means that an application must not construct a Scene or a Stage in the init method. An application may construct other JavaFX objects in the init method.


1 Answers

Because I used id while was to use fx:id tag.

like image 114
Suzan Cioc Avatar answered Nov 03 '22 10:11

Suzan Cioc