简体   繁体   中英

JavaFX: How to set an action to a ComboBox?

ComboBox dropDown = new ComboBox();


dropDown.getItems().addAll("Mexican Peso", "Canadian Dollar", "Euro", "British Pound");

dropDown.setOnAction(e -> dropDownMenu());
private void dropDownMenu(ComboBox dropDown){


    }
private void calculatePound() {

    double n1 = Double.parseDouble(input.getText());
    output.setText(String.format("%.2f", n1*.80));
}

I'm trying to call a method for each individual element in the combobox. So for an example: if someone chose "British Pound" it would call the calculatePound method I have written and calculate the British Pound conversion. I haven't done much GUI so I'm attempting to get more practice with it.

The idiomatic way to do this is to create a class encapsulating the data and functionality you need, and to populate your combo box with instances of it.

Eg Create a Currency class:

public class Currency {

    private final double exchangeRate ;
    private final String name ;

    public Currency(String name, double exchangeRate) {
        this.name = name ;
        this.exchangeRate = exchangeRate ;
    }

    public double convert(double value) {
        return value * exchangeRate ;
    }

    public String getName() {
        return name ;
    }

    @Override
    public String toString() {
        return getName();
    }
}

Then do

ComboBox<Currency> dropDown = new ComboBox<>();
dropDown.getItems().addAll(
    new Currency("Mexican Peso", 18.49),
    new Currency("Canadian Dollar", 1.34),
    new Currency("Euro", 0.89),
    new Currency("British Pound", 0.77)
);

And now all you need to do is

dropDown.setOnAction(e -> {
    double value = Double.parseDouble(input.getText());
    double convertedValue = dropDown.getValue().convert(value);
    output.setText(String.format("%.2f", convertedValue));
});

You could add other currency-specific information to the Currency class, such as a currency symbol and formatting rule, etc.

Note how much easier it is to add more currencies with this approach (all you need is another new Currency(name, rate) in the dropDown.getItems().addAll(...) call), compared to the approach of populating the combo box with strings (where you would need another method and another case in your switch or if statement). There are many other benefits to this approach.

You'll first need a button of some sort for the user to click on after selecting an item in the list. This button should call a method that:

  1. Determines which item in the combobox list is currently selected
  2. Calls another method to perform the appropriate action on the current selected item

For example:

        ComboBox dropDown = new ComboBox();
        dropDown.getItems().addAll("Mexican Peso", "Canadian Dollar", "Euro", "British Pound");

        Button button = new Button();
        button.setOnAction(event -> {
              //Call a method to determine which item in the list the user has selected
              doAction(dropDown.getValue().toString()); //Send the selected item to the method
        });


  private void doAction(String listItem) {
        switch (listItem) {
              case "Mexican Peso": //Action for this item
                    break;
              case "Canadian Dollar": //Action for this item
                    break;
              case "Euro": //Action for this item
                    break;
              case "British Pound": calculatePound();
                    break;
              default: //Default action
                    break;
        }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM