Using initialize method in a controller in FXML?

2019-07-15 03:28发布

问题:

I have a Java application with a view written in FXML. I am wanting to select a default tab, so when the program launches the first tab is shown as selected. I have seen the best way to do this is create an initialize() method in the controller and annotate it with @FXML which should the be loaded. For some reason though, the method is never executed. Code is below.

MainApp.java

import controller.Controller;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import javax.swing.*;

    public class MainApp extends Application{

        private String osName;
        private Parent root;

        @Override
        public void start(Stage primaryStage) throws Exception {
            osName = System.getProperty("os.name").toString();

            if(osName.charAt(0) == 'W' || osName.charAt(0) == 'w') {
                root = FXMLLoader.load(getClass().getResource("/view/WindowsView.fxml"));
            } else if(osName.charAt(0) == 'M' || osName.charAt(0) == 'm'){
                root = FXMLLoader.load(getClass().getResource("/view/MacView.fxml"));
            }else{
                root = null;
            }

            if(root != null){
                Scene scene = new Scene(root);
                scene.getStylesheets().add(getClass().getResource("main.css").toExternalForm());
                primaryStage.setScene(scene);
                primaryStage.show();
            }else{
                JOptionPane.showMessageDialog(null, "Could not find OS, exiting program.", "Error", JOptionPane.ERROR_MESSAGE);
                System.exit(0);
            }
        }

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

relevant parts of Controller.java

    @FXML
    private TabPane myTabPane;

    @FXML
    private Tab defaultTab;

    @FXML
    private void initialize() {
        myTabPane.getSelectionModel().select(defaultTab);
    }

Relevant FXML

<TabPane fx:id="myTabPane" cache="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" nodeOrientation="RIGHT_TO_LEFT" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" tabMinHeight="25.0" tabMinWidth="100.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
 <Tab id="scanscleanup" fx:id="defaultTab" text="Scans/Cleanup">

回答1:

You must set the controller in FXML loading

There are two ways to do it:

1º way: Set the controller in FXMLLoader class. Instead of do that

FXMLLoader.load(getClass().getResource("/view/WindowsView.fxml"));

Do this

FXMLLoader loader = new FXMLLoader();
loader.setController(new Controller());
loader.setLocation(getClass().getResource("/view/WindowsView.fxml"));
root = loader.load();

2º way: Set controller in FXML

WindowsView.fxml

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

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.TextField?>

<AnchorPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.Controller">
  <Label text="This is my example in StackOverflow"/>
</AnchorPane>