You must hang an event handler:

public class CustomButton extends Button implements EventHandler<MouseEvent> { public CustomButton() { super("myButton"); } @Override public void handle(MouseEvent event) { if (event.getEventType() == MouseEvent.MOUSE_CLICKED){ setText("hello"); System.out.println("button pressed"); } } } 

main:

 public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ Stage stage = primaryStage; stage.setTitle("Simple Application"); stage.setScene(InitScene()); stage.show(); } private Scene InitScene(){ Group root = new Group(); Scene myScene = new Scene(root, 300, 300); root.getChildren().add (new CustomButton()); return myScene; } public static void main(String... args){ launch(args); } 

}

Problem: when you press a button, absolutely nothing happens.

    1 answer 1

    Java FX technology is designed to separate the graphical interface, the fxml file and event handling from the graphical interface. You implement a Java FX application in awt or swing style.

    To make an event handler you must:

    In the fxml file:

    1. Register fx: controller = "Class path" in the root element, where the function for handling the event will be described
    2. In the tag of the element to which we want to add an event, the onAction = "# Function Name" attribute is written in the handler class
    • thank you so much - Mihail Gogol