I actually need to perform a button click event upon pressing key enter as well as when mouse click is done.
Please Help me with the syntax if possible.
if i got it right this example guides you:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class DoubleEvent extends Application{
private Label label;
private int counter;
@Override
public void start(Stage primaryStage) throws Exception {
VBox root = new VBox(5);
label = new Label("press enter or button");
Button button = new Button("click me or press enter");
button.setOnAction(event -> myActionMethod());
root.getChildren().addAll(label, button);
primaryStage.setScene(new Scene(root, 200, 200));
primaryStage.show();
counter = 0;
}
private void myActionMethod() {
counter++;
label.setText("Event triggerd " + counter + " times");
}
public static void main(String[] args) {
Application.launch(args);
}
}
Focus on ".setOnAction()". Combine this with calling custom methods and the door is open for everything.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.