Calculate When Selecting Option In Enum Class

Asked

Viewed 77 times

1

Good Afternoon!!... I have the following question... I have an Enum class and in it within each "option" I perform a calculation when selecting... How would make that calculation run and so, I have a result?

see the code snippet:

public enum CoeficienteA implements EquacaoCamFinaInterface {

Polinomial {

            @Override
            public double calcular(Fruto fruto) {//Poli

                double a;
                a = fruto.getA1() + fruto.getA2() * fruto.getCondicao().getTempeSgm() + fruto.getA3() * Math.pow(fruto.getCondicao().getTempeSgm(), 2) + fruto.getA4() * Math.pow(fruto.getCondicao().getTempeSgm(), 3)
                + fruto.getA5() * Math.pow(fruto.getCondicao().getTempeSgm(), 4) + fruto.getA6() * Math.pow(fruto.getCondicao().getTempeSgm(), 5) + fruto.getA7() * Math.pow(fruto.getCondicao().getTempeSgm(), 6)
                + fruto.getA8() * Math.pow(fruto.getCondicao().getTempeSgm(), 7);
                return a;
            }
        },
Exponencial {
            @Override
            public double calcular(Fruto fruto) {
                double a;
                a = fruto.getA1() + Math.exp(-fruto.getA2() * fruto.getCondicao().getTempeSgm());
                return a;
            }
        };

}

I need that when I select a combobox item it calls the method, I tried to use the event that follows below but it gives error, because it does not recognize the class coefficients A (Coefficient newValue) as an object, I need it to run when I select, because I need the data returned by the function above.

 comboboxCoefA.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {


        public void changed(ObservableValue observable, Object oldValue, CoeficienteA newValue) {
            newValue.Exponencial.calcular(fruto);
        }


        }


    });
  • I need that when I select a combobox item it calls a method, that runs only when I select, because I need the data returned by the function.

1 answer

0

If you are using FXML is very simple, just add the method you want in the Combobox onAction event, it will run whenever you select any item in the Combo.

The example below will display the new selected combo value:

@FXML
public void comboOnAction() {
    System.out.println(combo.getValue());
}

Remembering to add in FXML the above method.

If you do not use FXML you can set the onAction event in the combo. The example below has the same result as the one above:

combo.setOnAction(new EventHandler<Event>() {

            @Override
            public void handle(Event event) {
                System.out.println(combo.getValue());
            }

        });

If you are using java 8 you can also use lambda to do this:

combo.setOnAction(event -> {
            System.out.println(combo.getValue());
        });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.