[英]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());
在不調用FXMLLoader
的load
方法的情況下,即使在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.