简体   繁体   中英

How to get combo box to say one thing but put in something else?

So I have a Combo box setup with some text, but I would like the text to say one thing when I get the value on the text I want it to put in a SQL statement I have written

Code example

      Label querydroplabel = new Label("Select from drop down");
      ComboBox<String> querydrop = new ComboBox<>();
      querydrop.getItems().addAll(
              "Sort cities by population / acending",
              "Sort cities by population / descending",
              "Sort cities by name",
              "Get total population",
              "Get AVG population",
              "Get Highest population",
              "Get Lowest population");
      querydrop.setOnAction(event ->
      {
          queryTextArea.setText(querydrop.getValue());
      });

So when I select say the first one (Sort cities by population / ascending) I want to get value but I want that value to be SELECT cityname FROM City ORDER BY population ASC

To put it simply, you may use an enum:

import java.util.Arrays;

public enum ComboSQL {
    SORT_CITY_POPULATION_ASC(
        "Sort cities by population / ascending",
        "SELECT cityname FROM City ORDER BY population ASC"),
    SORT_CITY_POPULATION_DESC(
        "Sort cities by population / descending",
        "SELECT cityname FROM City ORDER BY population DESC"),
    SORT_CITY_NAME        ("Sort cities by name",    "SELECT cityname FROM City ORDER BY cityname"),
    GET_TOTAL_POPULATION  ("Get total population",   "SELECT SUM(population) FROM City"),
    GET_AVERAGE_POPULATION("Get AVG population",     "SELECT AVG(population) FROM City"),
    GET_HIGHEST_POPULATION("Get Highest population", "SELECT MAX(population) FROM City"),
    GET_LOWEST_POPULATION ("Get Lowest population",  "SELECT MIN(population) FROM City");

    private String label;
    private String sql;

    private ComboSQL(String label, String sql) {
        this.label = label;
        this.sql = sql;
    }

    public String getLabel() {
        return this.label;
    }

    public String getSQL() {
        return this.sql;
    }

    public static ComboSQL getByLabel(String label) {
        return Arrays.stream(ComboSQL.values())
                .filter(e -> e.getLabel().equals(label))
                .findFirst().orElse(null);
    }
}

Then you should be able to populate combo using labels and then map selected element of the combo to the enum

      querydrop.getItems().addAll(
          Arrays.stream(ComboSQL.values())
              .map(ComboSQL::getLabel)
              .collect(Collectors.toList())
      );
      // ...
      querydrop.setOnAction(event ->
      {          
          queryTextArea.setText(ComboSQL.getByLabel(querydrop.getValue()).getSQL());
      });

It is also possible to find an SQL by the index:

querydrop.setOnAction(event ->
      {          
          int selectedIndex = querydrop.getSelectionModel().getSelectedIndex();
          if (selectedIndex > -1) {
              ComboSQL combo = ComboSQL.values()[selectedIndex];
              queryTextArea.setText(combo.getSQL());
          }
      });

I suppose a relatively simple solution would be to pull the required SQL String from a String[] Array based on the index value of the selected item within the Combo-Box, for example:

/* You can fill this array from a section in a text file or
   from a database table of SQL Strings, or whatever...   */
String[] sqlByComboIndex = {"SELECT cityname FROM City ORDER BY population ASC;",  
                            "SELECT cityname FROM City ORDER BY population DESC;",
                            "SELECT cityname FROM City ORDER BY name ASC;",
                            "SELECT SUM(population) FROM City;",
                            "SELECT AVG(population) FROM City;",
                            "SELECT cityname, MAX(population) AS HighestPopulation FROM City GROUP BY cityname ORDERED BY cityname;",
                            "SELECT cityname, MIN(population) AS LowestPopulation FROM City GROUP BY cityname ORDERED BY cityname;"
                            };
int comboIndex = jComboBox1.getSelectedIndex();
String sqlString = null;
if (comboIndex >= 0) {
    sqlString = sqlByComboIndex[comboIndex];
}

I give no guarantees on the queries. :)

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