0

I want to implement a Swing Input Dialog with different options shown in a combo box. My specific case is for Contact creation, the end user can choose between existing Contacts or create a new one himself.

So I've got this static method which basically returns a new instance of a JOptionPane, which has the available selection objects out of the box. Note this code creates, let's say the parent dialog, which offers selecting an existing contact or clicking on the button to create a new one:

enter image description here

/**
 * 
 * @param message
 *            Here I've got a JPanel which allows the end user to show-hide
 *            the Contact creation dialog
 * @param contacts
 *            Contact possibilities
 * @return reference to the created JOptionPane
 */
public static JOptionPane newContactOptionPane(Object message,
        Set<XmlContact> contacts) {
    Object[] contactPossibilities = new Object[contacts.size()];
    int index = 0;
    for (XmlContact contct : contacts) {
        contactPossibilities[index] = String.format("%s %s, %s",
                contct.get_Surname1(), contct.get_Surname2(),
                contct.get_Name());
        index++;
    }
    JOptionPane optPane = new JOptionPane();
    JOptionPane.showInputDialog(optPane, message, "Seleccionar Contacto",
            JOptionPane.QUESTION_MESSAGE, null, contactPossibilities,
            contactPossibilities[0]);
    return optPane;
}

The invoker code would be something like:

JOptionPane contactSelectionPane = 
    ViewUtils.newContactOptionPane(createContactPanel, xmlContacts);
XmlContact selectedContact = 
    (XmlContact) contactSelectionPane.getValue();

Later on, I would like to recover the selected value using JOptionPane#getValue() method.

The desired behaviour is to show the form for Contact creation when clicking Crear nuevo contacto, so the previous JDialog will be hidden:

enter image description here

I've got two reasons for keeping the reference at the invoker code, the first one is because I would like to wrap the option pane to make it return an XmlContact object instead of an String and having to search it again the possible options once and again in my invoker code. The other one is because I want to keep a reference of contactSelectionPane for enabling a button into createContactPanel to show/hide it.

Now the contactSelectionPane.getValue() is obviously returning an String, which forces me to check the options again. How can I implement that?

Aritz
  • 30,971
  • 16
  • 136
  • 217

2 Answers2

1

Why don't you do something like this:

public class Option<X> {
    private final X value;
    private final String name;

    public String toString() {
         return name;
    }

    public X getValue() {
         return value;
    }

    public Option(X value, String name) {
         this.value=value;
         this.name=name;
    }
}

public static JOptionPane newContactOptionPane(Object message,
        Set<XmlContact> contacts) {
    Object[] contactPossibilities = new Object[contacts.size()];
    int index = 0;
    for (XmlContact contct : contacts) {
        contactPossibilities[index] = new Option<XmlContact>(contct, String.format("%s %s, %s",
                contct.get_Surname1(), contct.get_Surname2(),
                contct.get_Name()));
        index++;
    }
    JOptionPane optPane = new JOptionPane();
    JOptionPane.showInputDialog(optPane, message, "Seleccionar Contacto",
            JOptionPane.QUESTION_MESSAGE, null, contactPossibilities,
            contactPossibilities[0]);
    return optPane;
}

And later you do:

JOptionPane contactSelectionPane = 
    ViewUtils.newContactOptionPane(createContactPanel, xmlContacts);
XmlContact selectedContact = 
    ((Option<XmlContact>) contactSelectionPane.getValue()).getValue();

Hope that helps.

EDIT:

as for creating a new XmlContact I'd simply add an additional Option to the list of available options similar to new Option<XmlContact>(new XmlContact(...), "Create new contact...");.

stryba
  • 1,979
  • 13
  • 19
  • I definitely chose that way. The idea of wrapping an option for contact creation into the combo is just great! – Aritz Dec 16 '13 at 10:00
1

I would use a SelectionChangedListener in order to get the actual selected item from the JComboBox.

In order to get the new XmlContact if created I would use some kind of variable to remember the created XmlContact.

At last I would derive a new Class from JOptionPane in which I override the getValue method where I either get the new XmlContact or the selected one from the JComboBox. You then use this class rather than the pure JOptionPane.

Community
  • 1
  • 1
boutta
  • 24,189
  • 7
  • 34
  • 49