In: Computer Science
A JavaFX question
a method called generate2Num(Pane, pane){};
A botton "botton1" that botton.setOnAction(e-> {}); calling generate2Num method and show the numbers on Scene.
Every time the user clicks botton1, the updated number will show on the Scene.
I just wanna know how can I update my data by click the button.
Please show the code , thank you
Answer:-
************Main.java************
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root
= new BorderPane();
Button button1 =
new Button("updatenumber");
// action event
EventHandler<ActionEvent> event = new
EventHandler<ActionEvent>() {
public void handle(ActionEvent e)
{
generate2Num(root);
}
};
// when button is pressed
button1.setOnAction(event);
root.setTop(button1);
Scene scene =
new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
int number=0;
public void generate2Num(Pane pane){
number++;
BorderPane borderp= (BorderPane) pane;
borderp.setCenter(new Text(""+number));
}
public static void main(String[] args) {
launch(args);
}
}
************Main.java Output************
Description:-in the above example "i clicked the updatenumber button 80 times" the updated number is "80"
whenever you click on the "updatenumber" button the updated number will show on the Scene
***Please Hit The Like Button***