Answer the question
In order to leave comments, you need to log in
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="";
});
Button button=new Button();
AtomicReference<String> s = null;
button.setOnAction(e->{
s.set("");
});
Button button=new Button();
final String[] s = new String[1];
button.setOnAction(e->{
s[0] ="";
});
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);
}
}
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
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 questionAsk a Question
731 491 924 answers to any question