0

I'm making an application with JavaFX and Scene Builder.

I have two controllers:Controller and FontController

I have Main class that launch my program and open Stage with first fontroller (Controller)

public class Main extends Application {


public static void main(String[] args) {
launch(args);
}

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

  Parent root = FXMLLoader.load(getClass().getResource("/card/card.fxml"));
  Scene scene = new Scene(root, 1600, 600);
  primaryStage.setScene(scene);
  scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
  primaryStage.initStyle(StageStyle.UNDECORATED);
  primaryStage.setMaximized(true);
  primaryStage.setResizable(true);


  primaryStage.getIcons().add(new Image("card/resources/logo-icon.png"));
  primaryStage.show();

    //adding resize and drag primary stage
    ResizeHelper.addResizeListener(primaryStage);



  //assign ALT+ENTER to maximize window
  final KeyCombination kb = new KeyCodeCombination(KeyCode.ENTER, KeyCombination.CONTROL_DOWN);
  scene.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent event) {
      if (kb.match(event)) {
        primaryStage.setMaximized(!primaryStage.isMaximized());
        primaryStage.setResizable(true);

      }
    }
  });

} catch (Exception e) {
  e.printStackTrace();
}

}

}

There is a label and a button in Controller. When I click on the button a method is called and new window with second controller appears(FontController):

@FXML private Button btnFont;
@FXML  private Label category1
@FXML
 void changeFont(ActionEvent event) {
   try {
  FXMLLoader fxmlLoader = new 
FXMLLoader(getClass().getResource("font.fxml"));
  Parent rootFont = (Parent) fxmlLoader.load();
  Stage stage = new Stage();
  stage.setTitle("Select Font");
  stage.setScene(new Scene(rootFont));
  stage.show();    

} catch (Exception e) {
  System.out.println("can't load new window");
}

}

There is the button "OK" and label in FontCOntroller:

@FXML  private Label fontLabel;
@FXML  private Button btnFontOk;

Please tell me, what should I do to send and apply text from label in FontController when I click on the burtton "OK" to label in Controller?

Controller

FontController

kentforth
  • 489
  • 1
  • 6
  • 19
  • It might be easier to use a [`Dialog`](https://openjfx.io/javadoc/11/javafx.controls/javafx/scene/control/Dialog.html) as it's capable of returning a result. Otherwise, you'll have to communicate with the `FontController` instance from the `Controller` instance. You can get the controller instance via `FXMLLoader.getController`. – Slaw Dec 25 '18 at 16:26
  • @Slaw This window of FontController almost done. I didn't learn how to use DIalogs in JavaFX. It will be hard for me to place FontSelector, JFOenix Color Picker and other stuff in Dialog Window, so I've just decided to use other window with FXML. I don't understand how the code with getting FontController Instance will look like. Can you write me an example how to pass text from FontController to label of Controller? – kentforth Dec 25 '18 at 16:34
  • Possible duplicate of [Passing Parameters JavaFX FXML](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) – Slaw Dec 25 '18 at 17:21

1 Answers1

1

SOLUTION FOUND:

I created class "Context" in my project directory to make all controllers communicate each other. You can add as many controllers as you want there.

Here it looks like:

package card;


public class Context {
  private final static Context instance = new Context();
  public static Context getInstance() {
    return instance;
  }

  private Controller controller;
  public void setController(Controller controller) {
    this.controller=controller;
  }

  public Controller getController() {
    return controller;
  }

  private FontController fontController;
  public void setFontController(FontController fontController) {
    this.fontController=fontController;
  }

  public FontController getFontController() {
    return fontController;
  }
}

Controller:

I created getters and setters (ALT + Insert in IDEA) for Label that I wanna change

public Label getCategory1() {
    return category1;
  }

  public void setCategory1(Label category1) {
    this.category1 = category1;
  }

To get FontController variables and methods through Context class I placed line of code

//getting FontController through Context Class
  FontController fontCont = Context.getInstance().getFontController();

I registered Controller in Context class through my initialize method (my class implements Initializable)

@FXML
  public void initialize(URL location, ResourceBundle resources) {

    //register Controller in  Context Class
    Context.getInstance().setController(this);

  }

FontController:

to get Controller variables and methods I placed this code:

//getting Controller variables and methods through Context class
  Controller cont = Context.getInstance().getController();

I also registered FontController in Context class through initialize method:

@Override
  public void initialize(URL location, ResourceBundle resources) {

    //register FontController in  Context Class
    Context.getInstance().setFontController(this);
}

Method that send text and text color from label in this FontController to label in Controller when I click on button:

@FXML
  void applyFont(ActionEvent event) {
    cont.getCategory1().setText(fontLabel.getText());
    cont.getCategory1().setTextFill(fontLabel.getTextFill());
  }

*By creating Context class you can make controllers communicate each other and create as many controllers as you want there. Controllers see variables and methods of each other

kentforth
  • 489
  • 1
  • 6
  • 19