简体   繁体   中英

Java swing JOptionPane: combine showInputDialog and showOptionDialog?

I'm following the API here:

http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html#showOptionDialog%28java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object%29

I have a list of items I want to show in a ComboBox in a prompt window. The showInputDialog method from JOptionPane allows me to do just that, however it's limiting me to two buttons (ok and cancel). I would like to have more buttons (I can define more buttons, but I don't know how to add it to this window using showInputDialog).

I could use showOptionDialog to create an array of options (containing all the buttons I need), but then the prompt window cannot show my list of items. The parameter that normally accepts an array of items (in showInputDialog) is now expecting an array for the buttons.

Object[] selectionValues replaced by Object[] options

Is there a way to combine their functions?

I could technically create a new GUI that does this, but I might be doing a lot of unnecessary work if there's already an existing implementation to this. Also, it's important that the user completes the task on the new prompt window before being able to do anything on the original frame.

So in the end, this should happen:

  1. User clicks something and a prompt window appears.
  2. Prompt window shows a combobox (dropbox?) list of items.
  3. The user selects one of these items and can choose to press ok, cancel, skip, ... (any number of buttons that I have defined) 3a. The user cannot do anything on the original GUI until one of the buttons on the new prompt window have been clicked.
  4. When one of the buttons is clicked, it carries out it's function and the user can carry on on the main GUI.

You could create a JPanel that holds a JComboBox, and then place this into any JOptionPane that you'd like as the Object parameter.

eg,

import javax.swing.JComboBox;
import javax.swing.JOptionPane;

public class JComboFun {
   public static void main(String[] args) {
      String[] weekdays = { "Monday", "Tuesday", "Wednesday", "Thursday",
            "Friday" };
      final JComboBox<String> combo = new JComboBox<>(weekdays);

      String[] options = { "OK", "Cancel", "Fugedaboutit" };

      String title = "Title";
      int selection = JOptionPane.showOptionDialog(null, combo, title,
            JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
            options, options[0]);

      if (selection > 0) {
         System.out.println("selection is: " + options[selection]);
      }

      Object weekday = combo.getSelectedItem();
      if (weekday != null) {
         System.out.println("weekday: " + weekday);
      }

   }
}

I have a list of items I want to show in a ComboBox in a prompt window. The showInputDialog method from JOptionPane allows me to do just that, however it's limiting me to two buttons (ok and cancel). I would like to have more buttons (I can define more buttons, but I don't know how to add it to this window using showInputDialog).

  • your question is answered very well, but is possible to create a lots of JComponents without additional effort, then is possible, quite easy to put together bunch of JButtons and JComboBox together (both answers by HFOE and camickr) into one JOptionPane

  • :-)

.

import java.awt.EventQueue;  
import javax.swing.Icon;  
import javax.swing.JOptionPane;  
import javax.swing.UIManager;  

public class MyOptionPane {  

    public MyOptionPane() {  
        Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");  
        Object[] possibilities = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};  
        Integer i = (Integer) JOptionPane.showOptionDialog(null,   
                null,  "ShowInputDialog",   
                JOptionPane.PLAIN_MESSAGE, 1,  errorIcon, possibilities, 0);

        // or

        Integer ii = (Integer) JOptionPane.showInputDialog(null,  
                "Select number:\n\from JComboBox", "ShowInputDialog",  
                JOptionPane.PLAIN_MESSAGE, errorIcon, possibilities, "Numbers");  
    }  

    public static void main(String[] args) {  
        EventQueue.invokeLater(new Runnable() {  
            @Override  
            public void run() {  
                MyOptionPane mOP = new MyOptionPane();  
            }  
        });  
    }  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM