开发者

swing: selecting among a set of objects

开发者 https://www.devze.com 2023-01-22 21:59 出处:网络
I need a static utility method for selecting objects from a List<T>. I\'m running into two issues. Here\'s a short test program:

I need a static utility method for selecting objects from a List<T>. I'm running into two issues. Here's a short test program:

package com.example.test.gui;

import java.util.Arrays;
import java.util.List;
import javax.swing.JOptionPane;

public class SelectionTest {
    public interface NameExtractor<T> {
        public String extractName(T object);
    }

    static public <T> T selectFromList(String message, List<T> list, NameExtractor<T> nameExtractor) {
        String[] cho开发者_运维问答ices = new String[list.size()];
        int i = 0;
        for (T t : list)
            choices[i++] = nameExtractor.extractName(t);
        Object s = JOptionPane.showInputDialog(null, message, "",
                JOptionPane.QUESTION_MESSAGE, null, choices, null);
        System.out.println(s);
        // crap, we get a string back. Now how do we get back the object in question?
        return null;
    }

    static public void main(String[] args)
    {
        List<Integer> numbers = Arrays.asList(1,2,3,4,10);
        System.out.println(selectFromList("Pick one", numbers, new NameExtractor<Integer>(){
            @Override public String extractName(Integer object) {
                return object.toString();
            }
        }));
    }
}

Is there an alternative to the JOptionPane.showInputDialog() that would allow me to get the index of the list rather than a displayed String?

edit: I would also rather force the use a JList rather than a combo box or whatever the JOptionPane wants as a default.


Why don't you just put the objects into the JOptionPane instead of putting in an array of Strings? The JOptionPane will invoke the object's toString method for display purposes.

    Object selectedObject = JOptionPane.showInputDialog(null, message, "",
            JOptionPane.QUESTION_MESSAGE, null, list.toArray(), null);

    return (T)selectedObject;

OR:

Search through the names for the selected name and return the object:

    for(int j = 0 ; j < choices.length; j++){
        if(selectedObject.equals(choices[j])){
            return list.get(j);
        }
    }
    return null;


I ended up doing the following, which seems a little clunky, so if someone else comes up with a cleaner way to do this, I'll accept your answer.

/**
 * @param <T> element class
 * @param title dialog title
 * @param message message
 * @param list list
 * @param nameExtractor gets names from element
 * @return item from the list
 */
static public <T> T selectFromList(String title, String message, List<T> list, NameExtractor<T> nameExtractor) {
    JPanel p = new JPanel();
    JList jlist = new JList();
    DefaultListModel model = new DefaultListModel();
    for (T t : list)
        model.addElement(nameExtractor.extractName(t));
    jlist.setModel(model);
    p.setLayout(new BorderLayout());
    p.add(new JLabel(message), BorderLayout.NORTH);
    p.add(jlist, BorderLayout.CENTER);
    JOptionPane op = new JOptionPane(p, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
    JDialog d = op.createDialog(title);
    d.setVisible(true);
    Object sel = op.getValue();
    int i = jlist.getSelectedIndex();
    d.dispose();
    if (i != -1 && sel instanceof Integer && (Integer)sel == JOptionPane.OK_OPTION)
        return list.get(i);
    else
        return null;
}


Is there an alternative to the JOptionPane.showInputDialog() that would allow me to get the index of the list rather than a displayed String?

All of the option pane showXXX methods allow you to add any object directly to the option pane. So you can just add your list (scroll pane?) to the dialog.

JList list = new JList(...);
int value = JOptionPane.showOptionDialog(null, list, ...);

Read the section from the Swing tutorial on How to Use Dialogs. You can even prevent the dialog from closing until an item has been selected from the list, if you wish.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号