将两个字符串发送到 JOption 列表菜单

Sending Two Strings to JOption List Menu

所以我有这个:

while(results.next())               
            {               
                // Put into interactive list    
                a.add(hospital);
                hospital = results.getString("hospitalName");
                {   
                    // Loops each hospital via popup, needs to be added to a selection menu
                    //JOptionPane.showMessageDialog(null, hospital, "Hospital List", JOptionPane.INFORMATION_MESSAGE);
                    System.out.println(hospital);               
                } 
            }

                {
                // Displays list of hospitals
                  JOptionPane.showInputDialog(null, "Please choose a hospital", "Determined Hospitals",
                          JOptionPane.QUESTION_MESSAGE, null, new Object[] 
                          {hospital}, a);
                }

它将数组中的两家医院打印到控制台,所以我知道它找到了它们。但是,当我尝试通过 JOption 在列表框中向用户显示它们时,它只显示最新的(第二个)医院而不是第一个。

我是否跳过了第一个字符串?

您跳过第一个 hospital 的原因是因为这里有这一行:

JOptionPane.showInputDialog(null, "Please choose a hospital", "Determined Hospitals",
                      JOptionPane.QUESTION_MESSAGE, null, new Object[] 
                      {hospital}, a);

在这里您要添加一个新的 Object[],它只有选项 hospital,它被设置为第二个。您正在寻找的是:

JOptionPane.showInputDialog(null, "Please choose a hospital", "Determined Hospitals",
                      JOptionPane.QUESTION_MESSAGE, null, a, a[0]);

这会将选项设置为所有 a,并将初始值设置为 a 中的第一个 hospital

修复此问题:

List<String> a = new ArrayList<String>();
            String hospital = null;

            while (results.next()) 
            {
                // Put into interactive list    
                hospital = results.getString("hospitalName"); 
                {
                    // Loops each hospital via popup, needs to be added to a selection menu
                    //JOptionPane.showMessageDialog(null, hospital, "Hospital List", JOptionPane.INFORMATION_MESSAGE);
                    System.out.println(hospital);
                a.add(hospital);
                }
            }

            {
                // Add the hospital to the array of hospitals found
                Object[] options = a.toArray();

                // Give operator the choice of hospital suited for the patient             
                JOptionPane.showInputDialog(null, "Please choose a hospital", "Determined Hospitals", 
                         JOptionPane.QUESTION_MESSAGE,  null, options, options[0]);