Calculate When Select Option In Enum Class

1

Good afternoon !! ... I have the following doubt ... I have an Enum class and in it within each "option" I execute a calculation when selecting ... How would I do this calculation to be executed and thus, I have a result?

see the snippet of code:

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 to select a combobox item to call the method, I tried to use the following event but it gives error, because it does not recognize the class coefficientA (CoefficientA newValue) as an object, I need it to execute when I select , because I need the data returned by the above function.

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


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


        }


    });
    
asked by anonymous 29.05.2015 / 21:40

1 answer

0

If you are using FXML it is quite simple, just add the method you want in the onAction event of the ComboBox, it will run whenever any items in the Combo are selected.

The following example will display the new selected value of the combo:

@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 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());
        });
    
23.06.2015 / 03:34