S
S
Scorpio2019-04-22 09:14:52
JavaFX
Scorpio, 2019-04-22 09:14:52

Javafx, why does the event (not) work out like that?

I can't change external variables, intellij swears.

Button button=new Button();
        String s;
        button.setOnAction(e->{
            s="";
        });

Says it needs to be like this:
Button button=new Button();
        AtomicReference<String> s = null;
        button.setOnAction(e->{
            s.set("");
        });

Or like this:
Button button=new Button();
        final String[] s = new String[1];
        button.setOnAction(e->{
            s[0] ="";
        });

If I use FXML + controller , then there are no problems, I can easily change external variables, why does it work like that?
I suppose if I create some class, inherit it from EventHandler and override the event method, and put this very class as a listener into the button, then everything will work too.
Code:
Main.class
public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        FXMLLoader fxmlLoader =new FXMLLoader(getClass().getResource("my.fxml"));
        Parent root= fxmlLoader.load();
        LoginWindowController loginWindowController= fxmlLoader.getController();
        loginWindowController.initialise(primaryStage);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }


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

myController.class
public class myController{
@FXML
 public void searchButtonPressed(MouseEvent mouseEvent) {
CreateNewWindow newWindow = new CreateNewWindow();
newWindow.create();    

}

}

public class CreateNewWindow{
create(){
Stage stage =new Stage();
Vbox v= new Vbox();
Button button =new Button("Press me");
v.getChildren().add(button);
stage.setScene(new Scene(v));
stage.show();

String str;
button.setOnAction(e->{
str="";  //Ругается
//Так - же отсюда я не могу вызвать никакой метод, который требует параметры;
});

}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Alexandrov, 2019-04-22
@jamakasi666

There is not enough code to accurately explain the meaning of the behavior, but in general something like this.
- Don't forget that javafx spins in its own thread.
- declare your String as a class member .
- regarding javafx, take a look at SimpleStringProperty and other types created specially for convenience.
Well, specifically, you have the following situation.
you are using a lambda which is basically a normal "nested anonymous class" . In such situations, any variable must be final or of type Atomic. All this is due to synchronization problems.
Alternatively, you can use a slightly different approach and somewhere inside javafx (in the case of fxml) it works like this:

String s;
private void doSome(){
 s="";
}
Button button=new Button();
button.setOnAction(e->{
  doSome();
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question