Search code examples
javaarraysuser-interfacejdbcjoptionpane

Sending Two Strings to JOption List Menu


So I have this:

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);
                }

It prints both hospitals of the array to the console so I know it finds them. However when I try and display them to the user in a listbox through JOption, it only displays the latest (second) hospital and not the first.

Am I skipping the first string?


Solution

  • Fixed this with:

    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]);