在FXML中的控制器中使用Initialize方法? [英] Using initialize method in a controller in FXML?

查看:276
本文介绍了在FXML中的控制器中使用Initialize方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java应用程序,其视图以FXML编写.我想选择一个默认选项卡,因此在程序启动时,第一个选项卡显示为已选择状态.我已经看到最好的方法是在控制器中创建一个initialize()方法,并用@FXML对其进行注释,该方法应该被加载.但是由于某种原因,该方法永远不会执行.代码在下面.

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

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);
        }
    }

Controller.java的相关部分

relevant parts of Controller.java

    @FXML
    private TabPane myTabPane;

    @FXML
    private Tab defaultTab;

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

相关的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">

推荐答案

您必须在FXML加载中设置控制器

You must set the controller in FXML loading

有两种方法可以做到:

1º方式:在FXMLLoader类中设置控制器.而不是这样做

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

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

执行此操作

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

2º方式:在FXML中设置控制器

2º way: Set controller in FXML

WindowsView.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>

这篇关于在FXML中的控制器中使用Initialize方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆