Jose Alberto Santiago Luna Jose Alberto Santiago Luna - 1 year ago 217
Java Question

How to Multiple setOnAction on the same button JavaFX

I'm trying to make the button jugar validates and returns value from a textbox and 3 drop boxes, there must be a way to validate more than one values, for example, the login button, when you type username and password.

here is the code

package application;

import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;

public class Main extends Application implements EventHandler<ActionEvent>{

// crea los botones
Button historial;
Button jugar;

public static void main(String[] args) {

//nombre de la ventana principal
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Football Simulator 2016");

here the drop lists

//crea los drop list
ChoiceBox<String> jugadasArbitro = new ChoiceBox<>();
jugadasArbitro.getItems().addAll("Saque de banda","Tiro de esquina","Falta","Falta(Tarjeta amarilla)", "Falta (Tarjeta roja)", "Mano","Posicion adelantada","Penal","Fuera de juego", "Gol");
ChoiceBox<String> jugadasOfensivas = new ChoiceBox<>();
jugadasOfensivas.getItems().addAll("Pase","Pase largo","Tiro a puerta");
ChoiceBox<String> jugadasDefensivas = new ChoiceBox<>();

here i have setOnAction listeners

//inicializa botones
TextField tiempo = new TextField();
historial = new Button("Historial");
jugar= new Button("Jugar");
jugar.setOnAction(e -> isInt(tiempo,tiempo.getText()));
jugar.setOnAction(e -> getArbitro(jugadasArbitro));
jugar.setOnAction(e -> getOfensiva(jugadasOfensivas));
jugar.setOnAction(e -> getDefensa(jugadasDefensivas));

//forma del Gui y posiciona los botones
GridPane grid = new GridPane();
grid.setPadding(new Insets(10,10,10,10));
GridPane.setConstraints(historial, 1, 3);
GridPane.setConstraints(jugar, 2, 2);
GridPane.setConstraints(jugadasArbitro, 3, 3);
GridPane.setConstraints(jugadasOfensivas, 3, 4);
GridPane.setConstraints(jugadasDefensivas, 3, 5);
grid.getChildren().addAll(historial, jugadasArbitro,jugadasDefensivas,jugadasOfensivas,jugar,tiempo);

//tamanio de la ventana
Scene scene = new Scene(grid, 540,300);

the methods to read the drop box value

//leen el valor de los drop box
private void getDefensa(ChoiceBox<String> jugadasDefensivas) {
// TODO Auto-generated method stub
String opcionDefensa = jugadasDefensivas.getValue();

private void getOfensiva(ChoiceBox<String> jugadasOfensivas) {
// TODO Auto-generated method stub
String opcionOfensiva = jugadasOfensivas.getValue();

private void getArbitro(ChoiceBox<String> jugadasArbitro) {
// TODO Auto-generated method stub
String opcionArbitro = jugadasArbitro.getValue();

//valida que el tiempo solo sea INT
private boolean isInt(TextField tiempo, String message) {
// TODO Auto-generated method stub
int minutos = Integer.parseInt(tiempo.getText());
return true;
}catch (NumberFormatException e){}
System.out.println("Error: introduzca numeros solamente");
return false;


public void handle(ActionEvent event) {



Answer Source

Can't you just have it all in one OnActionHandler? Or if you want it to only process one of them have a variable determine which to test (would be changed through other events and interactions).

jugar.setOnAction(e ->{


jugar.setOnAction(e -> {
    if(method == 1)
    else if(method == 2)
    else if(method == 3)
    else if(method == 4)

Just depends on how you need it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download