簡體   English   中英

前階段場景中的Java FX更改標簽文本

[英]Java FX change Label text in a previous stage scene

我有一個Main類啟動我的應用程序,它在fxml中指定了MainController類。 單擊“ Connect按鈕時, Connect打開另一個具有不同場景和控制器的窗口。 基於操作,我想通過MainController更改Label文本值,但是無法按預期工作。 請參閱下面的詳細信息。

基本上,我想從ConnectController類更新MainController類中connectedLabel上的文本,它不起作用。

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方法時,我在做什么錯?

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

在不調用FXMLLoaderload方法的情況下,即使在fxml文件中指定了fx:controller屬性,也不會創建任何控制器實例。

但是,在getController之前調用load並沒有幫助,因為fxml只是使用另一個控制器實例再次加載。

您需要“告訴” ConnectController有關創建它的MainController信息。 (請參閱傳遞參數JavaFX FXML

一種方法是將此代碼添加到ConnectController

private MainController mainController;

public void setMainController(MainController mainController) {
    this.mainController = mainController;
}

並使用此字段代替connectToken_onMouseClicked()方法中的局部變量。

要調用設置器,請在將視圖加載到connectTokenButton_onMouseClicked()后訪問控制器:

FXMLLoader loader = new FXMLLoader(getClass().getResource("ConnectView.fxml"));
AnchorPane page = loader.load();
loader.<ConnectController>getController().setMainController(this);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM