[英]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.