简体   繁体   English

如何让组合框说一件事但放入另一件事?

[英]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所以我有一个带有一些文本的组合框设置,但是当我得到文本上的值时,我希望文本说一件事,我希望它把它放在我写的 SQL 语句中

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因此,当我 select 说第一个(按人口/升序对城市进行排序)时,我想获得价值,但我希望该价值是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然后你应该能够使用标签填充组合,然后 map 选择的组合元素到枚举

      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:也可以通过索引找到 SQL:

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:我想一个相对简单的解决方案是根据组合框中所选项目的索引值从 String[] 数组中提取所需的 SQL 字符串,例如:

/* 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.我对查询不做任何保证。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对于JTable的一列,如何在每一行中放置一个唯一的组合框编辑器? - For one column of a JTable, how do I put a unique combo box editor in each row? 如何在combo_box中获取数据库值? - How to get database values in combo_box? 如何获取组合框中所选数据的值? - how to get the value of selected data in combo box? 您如何在Java的if / else语句之间放置一些内容? - How can you put something in between an if/else statement in java? 一个模型如何更新,例如在Cassandra CQL3中表示事物的“状态”,并能够查询该状态? - How would one model somthing updateable, say like a “status” of a thing in Cassandra CQL3 and be able to query on this status? 如何填充组合框? - How to pad a combo box? Java GUI GridBagLayout 如何在一列中容纳三个组合框? - Java GUI GridBagLayout how to fit three combo box in one column? 如何在Java中的按钮单击上显示一个组合框? - How to get a combo box to show on button click in java? 我如何将供应商名称放在我的sql表中,以一个数组,以便可以将其用于组合框? - how do i put the supplier names on my sql table to an array so that i may use it for a combo box? 如何将颜色放入组合框 JFrame 开关盒数组中? - How can I put color in combo box JFrame switch case array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM