Java FX更改前一阶段场景中的标签文本

时间:2016-12-04 14:31:57

标签: java javafx controller label fxml

我有一个Main类启动我的应用程序,它在fxml中指定了MainController类。单击Connect按钮时,将打开另一个具有不同场景和控制器的窗口。根据操作,我想通过Label更改MainController文本值,但它无法按预期工作。请参阅下面的详细信息。

基本上我想更新来自connectedLabel课程的MainController课程ConnectController上的文字,但这不起作用。

Main.java

public class Main extends Application {

    private static final Logger logger = Logger.getLogger(Main.class.getName());

    @Override
    public void start(Stage primaryStage) {
        try {
            logger.info("Application is starting");
            AnchorPane page = FXMLLoader.load(getClass().getResource("Main.fxml"));

            //BorderPane root = new BorderPane();
            //Scene scene = new Scene(root,400,400);

            Scene scene = new Scene(page);
            scene.getStylesheets().add(getClass().getResource("Main.css").toExternalForm());
            primaryStage.setScene(scene);

            primaryStage.setResizable(false);

            primaryStage.show();

        } catch(Exception e) {
            logger.warning(e.getMessage());
        }
    }

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

}

MainController.java

    public class MainController implements Initializable {

    private Context context = null;

    @FXML
    Label connectedLabel;
    @FXML
    Button connectButton;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        context = Context.getInstance();
    }

    public void setConnectedLabel(String name) {
        connectedLabel.setText(name);
        connectButton.setText("Disconnect");
    }

    @FXML
    public void connectTokenButton_onMouseClicked() {
        try {
            if (connectTokenButton.getText().equals("Disconnect")) {
                boolean disconnected = context.getToken().disconnectToken();
                if (disconnected) {
                    Alert alert = new Alert(AlertType.INFORMATION);
                    alert.setTitle("Disconnected");
                    alert.setHeaderText(null);
                    alert.setContentText("Succcessfully disconnected!");

                    alert.showAndWait();

                    connectedTokenLabel.setText("N/A");
                    connectTokenButton.setText("Connect");
                }
            } else {
                AnchorPane page = FXMLLoader.load(getClass().getResource("ConnectView.fxml"));

                Stage stage = new Stage();

                Scene scene = new Scene(page);
                scene.getStylesheets().add(getClass().getResource("ConnectView.css").toExternalForm());
                stage.setScene(scene);

                stage.setResizable(false);
                stage.initModality(Modality.APPLICATION_MODAL);
                stage.initOwner(connectedLabel.getScene().getWindow());
                stage.show();

                //Stage thisStage = (Stage) connectedTokenLabel.getScene().getWindow();
                //thisStage.close();
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

ConnectController.java

public class ConnectController implements Initializable {

    private Context context = null;

    @FXML
    ComboBox<String> selectComboBox;
    @FXML
    PasswordField userPinPasswordField;
    @FXML
    Button cancelButton;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        context = Context.getInstance();
    }

    public void setMainC(Stage stage) {
        mainStage = stage;
    }

    @FXML
    private void connectToken_onMouseClicked() {
        String pin = userPinPasswordField.getText();
        boolean connected = context.connect(selectComboBox.getValue(), pin);        
        if (connected) {

            Alert alert = new Alert(AlertType.INFORMATION);
            alert.setTitle("Connected");
            alert.setHeaderText(null);
            alert.setContentText("Succcessfully connected!");

            alert.showAndWait();

            FXMLLoader myLoader = new FXMLLoader(getClass().getResource("Main.fxml"));
            MainController mainController = myLoader.getController();
            mainController.setConnectedTokenLabel(context.getConnectedName());

            Stage thisStage = (Stage) selectComboBox.getScene().getWindow();
            thisStage.close();
        }
    }
}

从不同的控制器调用setConnectedLabel方法时我做错了什么?

1 个答案:

答案 0 :(得分:0)

load

不调用FXMLLoader的{​​{1}}方法,即使在fxml文件中指定了fx:controller属性,也不会创建控制器实例。

然而,在load之前调用getController将无济于事,因为fxml只是使用不同的控制器实例再次加载。

你需要&#34;告诉&#34; ConnectController关于MainController创建的ConnectController。 (见Passing Parameters JavaFX FXML

一种方法是将此代码添加到private MainController mainController; public void setMainController(MainController mainController) { this.mainController = mainController; }

connectToken_onMouseClicked()

并使用此字段代替connectTokenButton_onMouseClicked()方法中的局部变量。

要调用setter,请在FXMLLoader loader = new FXMLLoader(getClass().getResource("ConnectView.fxml")); AnchorPane page = loader.load(); loader.<ConnectController>getController().setMainController(this);

中加载视图后访问控制器
{{1}}