【问题标题】:Keep a reference of JOptionPane returning an specific class保留返回特定类的 JOptionPane 的引用
【发布时间】:2013-12-13 22:19:30
【问题描述】:

我想实现一个 Swing 输入对话框,在组合框中显示不同的选项。我的具体案例是Contact 创建,最终用户可以在现有Contacts 之间进行选择,也可以自己创建一个新的。

所以我有了这个静态方法,它基本上返回一个JOptionPane 的新实例,它具有开箱即用的可用选择对象。请注意,这段代码创建了父对话框,它提供了选择现有联系人或单击按钮来创建新联系人的功能:

/**
 * 
 * @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;
}

调用程序代码类似于:

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

稍后,我想使用JOptionPane#getValue() 方法恢复选定的值。

希望的行为是在点击Cear nuevo contacto时显示用于创建联系人的表单,因此之前的JDialog将被隐藏:

我有两个原因将引用保留在调用程序代码中,第一个是因为我想包装选项窗格以使其返回 XmlContact 对象而不是字符串并且必须再次搜索它在我的调用程序代码中一次又一次地出现可能的选项。另一个是因为我想保留contactSelectionPane 的引用,以便在createContactPanel 中启用按钮来显示/隐藏它。

现在contactSelectionPane.getValue() 显然返回了String,这迫使我再次检查选项。我该如何实现?

【问题讨论】:

    标签: java swing


    【解决方案1】:

    你为什么不做这样的事情:

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

    然后你会这样做:

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

    希望对您有所帮助。

    编辑:

    至于创建一个新的XmlContact,我只需在类似于new Option&lt;XmlContact&gt;(new XmlContact(...), "Create new contact..."); 的可用选项列表中添加一个额外的Option

    【讨论】:

    • 我绝对选择了这种方式。将用于创建联系人的选项包装到组合中的想法非常棒!
    【解决方案2】:

    我会使用SelectionChangedListener 来从 JComboBox 中获取实际选定的项目。

    如果创建了新的 XmlContact,我会使用某种变量来记住创建的 XmlContact。

    最后,我将从 JOptionPane 派生一个新类,在该类中我覆盖 getValue 方法,在该方法中我要么获取新的 XmlContact,要么从 JComboBox 中选择一个。然后使用这个类而不是纯 JOptionPane。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多