V
V
Verg1l2015-01-23 18:34:57
Java
Verg1l, 2015-01-23 18:34:57

How to call a method on button click in JavaFX interface?

I made an interface in javafx, now it is necessary that when a button is pressed in the interface, the desired method is called.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene Sharma, 2015-01-24
@Verg1l

1. Create an inner event
class class MyEvent< T > implements EventHandler {
public void handle(Event arg0) {
Call Method();
}}
2. Call the setOnAction method for the button and pass it an object of the event class as a parameter.
button.setOnAction(new MyEvent< ActionEvent >());
Or if you only need it once, you can use the anonymous button class
. setOnAction(new EventHandler< ActionEvent >() {
public void handle( ActionEvent e ) {
Method Call();
}
});

A
Andrey Tolstoy, 2015-02-09
@anzood

If Java 8, you can use lambda expressions:
button.setOnAction(arg0 -> YourMethod());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question