Java FX更改前一阶段场景中的标签文本 [英] Java FX change Label text in a previous stage scene
问题描述
我有一个 Main
类启动我的应用程序,该类具有在fxml中指定的 MainController
类。单击连接
按钮时,将打开另一个具有不同场景和控制器的窗口。基于操作我想通过我的 MainController
更改 Label
文本值,但它不能按预期工作。请参阅下面的详细信息。
I have a Main
class starting my application which has its MainController
class specified in fxml. When clicking on Connect
button another windows with different scene and controller is opened. Based on action I would like to change Label
text value through my MainController
, but it does not work as expected. See details below.
基本上我想在 MainController中更新
connectedLabel
上的文字/ code>来自 ConnectController
类的类,它不起作用。
Basically I would like to update text on connectedLabel
in MainController
class from ConnectController
class and it does not work.
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
方法?
What I am doing wrong when calling setConnectedLabel
method from different controller?
推荐答案
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("Main.fxml"));
MainController mainController = myLoader.getController();
mainController.setConnectedTokenLabel(context.getConnectedName());
不调用加载
方法 FXMLLoader
,即使在fxml文件中指定了 fx:controller
属性,也不会创建控制器实例。
Without calling the load
method of the FXMLLoader
, no controller instance is created, even if the fx:controller
attribute is specified in the fxml file.
然而,在 getController
之前调用加载
将无济于事,因为fxml刚刚使用不同的控制器实例加载。
However calling load
before getController
will not help, since the fxml is just loaded again with a different controller instance.
你需要告诉 ConnectController
关于 MainController
它是从中创建的。 (参见传递参数JavaFX FXML )
You need to "tell" the ConnectController
about the MainController
it was created from. (see Passing Parameters JavaFX FXML)
一种方法是将此代码添加到 ConnectController
类
One way would be to add this code to the ConnectController
class
private MainController mainController;
public void setMainController(MainController mainController) {
this.mainController = mainController;
}
并使用此字段代替 connectToken_onMouseClicked()
方法。
and use this field instead of the local variable in the connectToken_onMouseClicked()
method.
要调用setter,请在 connectTokenButton_onMouseClicked()中加载视图后访问控制器
:
To call the setter, access the controller after loading the view in connectTokenButton_onMouseClicked()
:
FXMLLoader loader = new FXMLLoader(getClass().getResource("ConnectView.fxml"));
AnchorPane page = loader.load();
loader.<ConnectController>getController().setMainController(this);
这篇关于Java FX更改前一阶段场景中的标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!