首页 > 解决方案 > String[] 选项只显示一次

问题描述

显然该选项只显示一次,然后对每个位置重复该过程。我希望它一次显示所有选项,我假设我需要循环遍历 String[] 选项。有任何想法吗?

List < Location > locations = eventController.listLocations();

for (Location locationCategories: locations) {

    String[] choices = {
        locationCategories.getLocationName()
    };

    String locationName = (String) JOptionPane.showInputDialog(null, "Område navn der skal tilføjes til eventet",
        "Område", JOptionPane.QUESTION_MESSAGE, null,
        choices,
        choices[0]);

标签: javachoice

解决方案


了解您希望在单个JOptionPane.
如果我理解正确,请尝试以下代码(使用流 API

/*
 * import java.util.stream.Collectors;
 */
List<Location> locations = eventController.listLocations();
String[] choices = locations.stream()
                            .map(Location::getLocationName)
                            .collect(Collectors.toList())
                            .toArray(new String[]{});
String locationName = (String) JOptionPane.showInputDialog(null,
                                                           "Område navn der skal tilføjes til eventet",
                                                           "Område",
                                                           JOptionPane.QUESTION_MESSAGE,
                                                           null,
                                                           choices,
                                                           choices[0]);

推荐阅读