I am teaching my self java(fx) in order to create some applications for my small business.
However I am stuck with the problem below. The actual program is much larger and complicated so I created a new, smaller and easier on the eye class to get some help for something that has me stumped for more than a week
The code below creates 3 instances of a label with different ids (in case it can help). Is there any way to change the text of (let's say) Label 2 to something else by pressing the button?
This is really my last hope to find a solution.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Test extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Change Label 2 text to something else");
btn.setLayoutX(220);
btn.setLayoutY(250);
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
//????????????????????????????????????
}
});
Pane root = new Pane();
root.getChildren().add(btn);
for(int i=1; i<=3; i++){
Label label = new Label("Label "+i);
label.setId(String.valueOf(i));
label.setLayoutX(i*60);
label.setLayoutY(i*60);
root.getChildren().add(label);
}
Scene scene = new Scene(root, 600, 500);
primaryStage.setTitle("Instances");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}