L
L
lonata2018-12-25 18:35:32
Java
lonata, 2018-12-25 18:35:32

How to send text from the label of one controller to the label of another controller in JavaFX?

I'm writing a program with JavaFX and Scene Builder
There is a main class Main which launches the program and opens the main controller window (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();
    }

  }

}

The main window has a label and also a button, when clicked, a window with another controller (FontController) appears
@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");
    }

  }

The FontController has a label and an "OK" button:
@FXML  private Label fontLabel;
@FXML  private Button btnFontOk;

Can you please tell me how to make sure that when you click on the OK button, the text from the label of this controller is sent to the Controller and displayed in the label?
hf4WX.jpgEoOhs.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sergey, 2018-12-25
kuzmin @sergueik

everything will have to be done by hand through getUserData / setUserData
www.java2s.com/Tutorials/Java/JavaFX/0430__JavaFX_...
they are on paper only for some in fact a lot for which widgets works,

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question