Trying to get an editable JComboBox to work on a JOptionPane

I am trying to avoid all of the work necessary to get a JDialog to look nice like a JOptionPane, but of course, JOptionPane doesn't explicitly offer an editable JComboBox.

As you know, you can get a JOptionPane to display a field that looks like a non-editable JComboBox with a call like the following (taken from "How to make Dialogs" (http://java.sun.com/j2se/1.5.0/docs/api/)):

Object[] possibilities ={"ham","spam","yam"};

String s = (String)JOptionPane.showInputDialog(

frame,

"Complete the sentence:\n"

+"\"Green eggs and...\"",

"Customized Dialog",

JOptionPane.PLAIN_MESSAGE,

icon,

possibilities,

"ham");

All I want to do is make the apparent JComboBox editable, which, of course, means that in addition to choosing one of the available options, you can enter your own.

Is there a reasonably straightforward way of doing this? I am willing to extend JOptionPane and JDialog to achieve this, but obviously, I don't want to have to duplicate most of the routines with new versions for an editable JComboBox.

An alternative is for me to break the input up into three dialogs where the first dialog asks something like "Do you want to enter a new value or choose from previously used valid values?" and then go to one of the other two dialogs based on the answer. Obviously, I would be sacrificing ease-of-use for ease of programming if I did this...

Thanks in advance for your advice.

[1718 byte] By [j.malonea] at [2007-10-3 3:08:25]
# 1
Sorry.. the "How to Make Dialogs" link is http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html
j.malonea at 2007-7-14 20:58:55 > top of Java-index,Desktop,Core GUI APIs...
# 2

> JOptionPane doesn't explicitly offer an editable JComboBox.

seems to works OK like this

import javax.swing.*;

class Testing

{

public Testing()

{

JComboBox cbo = new JComboBox(new String[]{"London","Rome","Sydney"});

cbo.setEditable(true);

JOptionPane.showMessageDialog(null, cbo, "Pick a City",-1);

JOptionPane.showMessageDialog(null,"You picked : "+((JTextField)cbo.getEditor().getEditorComponent()).getText());

System.exit(0);

}

public static void main(String[] args){new Testing();}

}

Michael_Dunna at 2007-7-14 20:58:55 > top of Java-index,Desktop,Core GUI APIs...
# 3

That looks so simple that I really feel stupid. I guess I didn't realize that I could put a swing component that accepts input in the message parameter of JOptionPane.show...Dialog() calls.

Now that I know that I can do this, I can use them much more effectively. Somehow I read "How to Make DIalogs" twice and looked at the all of the examples referred to therein without coming to this understanding.

Thanks very much for your help.

j.malonea at 2007-7-14 20:58:55 > top of Java-index,Desktop,Core GUI APIs...