-2

inside controller there is a TextArea

the application to check if it is already running just exits without letting another instance start

    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);
    }

that is a controller

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

public class Controller {
@FXML
private TextArea textArea;

public void doSomeThing(String myText) {
    textArea.setText(myText);
}
}
Armen
  • 103
  • 6
  • You need the `Controller` from the `FXMLLoader` – Raw Jun 30 '19 at 18:51
  • @Raw may be code show how to implement it? – Armen Jun 30 '19 at 19:00
  • Post your `Controller` ,than a can help you – Raw Jun 30 '19 at 19:14
  • So i have done an example just rename `MyController` to `Controller` – Raw Jun 30 '19 at 19:32
  • Then declare `private static Controller` and then use it in the main – Raw Jun 30 '19 at 19:44
  • See edit answer – Raw Jun 30 '19 at 19:46
  • Just show me your edit class where you done that what i have wrote in the answer – Raw Jun 30 '19 at 20:24
  • You should just add this Line `controller = fxmlLoader.getController();` under `AnchorPane anchorPane = fxmlLoader.load..` and in the main methode only call `controller.doSomeThing("myText")` – Raw Jun 30 '19 at 21:33
  • I think you need to reword your question. You are trying to determine if an instance of your **application** is already running before allowing another one to start? Because you seem to be asking at least two questions. You also seem to want one application to access the `TextField` of another? – Zephyr Jun 30 '19 at 22:07
  • And instead of posting a very lengthy back-and-forth conversation, please [edit] your question to include a [mcve] (actually read that link, please). That is the best way for us to better understand what you're trying to accomplish. – Zephyr Jun 30 '19 at 22:10
  • If it was clear, you'd have an answer and the question wouldn't be closed. Drop the attitude, read the rules and guidelines here, and try again. – Zephyr Jun 30 '19 at 22:50
  • Believe what you want. You didn't get an answer, the question is closed, and I've explained why. Good luck getting help. – Zephyr Jun 30 '19 at 22:57
  • @Raw Thanks, the answer came in handy in another code) – Armen Jul 02 '19 at 23:29

1 Answers1

-1

So a small example for you:

private static Controller controller;


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

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

And in the Controller define a public function.

MyController :

public void doSomeThing(String myText) {
    myTextField.setText(myText);
}
piet.t
  • 11,718
  • 21
  • 43
  • 52
Raw
  • 461
  • 7
  • 15