There are two classes, in the main window there is a button and a label, the button launches the method, which in turn using a static method of another class opens a new window to String txt, this txt should be changed in the second class method, using the button, but since in lambda, you can wield only final or effective-final variables, I created a temporary String which I then try to assign to the transmitted value, but it does not work.
public class JavaFXApplication1 extends Application { String post = "no changes"; Label lbl = new Label("no changes"); @Override public void start(Stage primaryStage) { Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction((ActionEvent event) -> { SomeChanger.changeSome(post); lbl.setText(post); }); VBox root = new VBox(); root.getChildren().addAll(btn,lbl); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } } public class SomeChanger { static String temporarySt = "no changes"; public static void changeSome(String st){ //st=temporarySt; Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction((ActionEvent event) -> { temporarySt = "changed st"; System.out.println("bla"); }); VBox root = new VBox(); root.getChildren().add(btn); Scene scene = new Scene(root, 300, 250); Stage primaryStage = new Stage(); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.showAndWait(); st=temporarySt; } } There are only a few mandatory conditions that can not be changed:
- Non-static variable is passed to the static method of another class.
- The method of this class should open a dialog box.
- The same method should via the button replace the variable being transferred so that it changes in the first grade.
changeSomewill end only after the window is closed, so you can simply make areturn temporaryStand in the calling codelbl.setText( SomeChanger.changeSome(post) );. Instead of a static field to get a result from lambda, it is better to use a local array, although the difference in this case is not great. - zRrr pm