I'm following the API here:
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:
You could create a JPanel that holds a JComboBox, and then place this into any JOptionPane that you'd like as the Object parameter.
e.g.,
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);
}
}
}