如何访问已经启动的应用程序领域

时间:2019-06-30 18:23:37

标签: javafx javafx-8

内部控制器中有一个TextArea

要检查是否已在运行的应用程序只是退出而没有启动另一个实例

    public class Main extends Application {

    private static Controller controller;

    @Override
    public void start(Stage primaryStage) throws Exception{

    FXMLLoader fxmlLoader = new FXMLLoader();
    AnchorPane anchorPane = fxmlLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setScene(new Scene(anchorPane));
    primaryStage.setTitle("Hello World");
    primaryStage.show();
}

public static void main(final String[] args) throws IOException {

    if (Application Launch)) {
       //how to access the open application


       FXMLLoader fxmlLoader  = new FXMLLoader(getClass().getResource("sample.fxml"));
       controller = fxmlLoader.getController();
       controller.doSomeThing("myText");

        System.exit(0);
    }else {
        launch(args);
    }

那是控制者

import javafx.fxml.FXML;
import javafx.scene.control.TextArea;

public class Controller {
@FXML
private TextArea textArea;

public void doSomeThing(String myText) {
    textArea.setText(myText);
}
}

1 个答案:

答案 0 :(得分:-1)

为您提供一个小例子:

private static Controller controller;


FXMLLoader fxmlLoader  = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent anchorPane = fxmlLoader.load();

controller = fxmlLoader.getController();
controller.doSomeThing("myText");
...

然后在Controller中定义一个public函数。

MyController:

public void doSomeThing(String myText) {
    myTextField.setText(myText);
}