0

可能重复:
如何在文本字段中输入时列出建议

有没有办法在 java swing 中的 JTextField 中获取预测文本?就像如果我想从用户那里获取城市的名称,那么它应该预测城市。

4

3 回答 3

1

SwingX 提供自动完成功能:http ://swingx.java.net/

于 2013-02-04T17:32:03.663 回答
0

我的一些程序中有一个自动完成功能。不幸的是,我无法找到我在互联网上获得信息的地方。我发布了我拥有的代码,但我不是这个 AutoCompleteDocument 类的“原始”作者。如果您在互联网上找到它,请给我链接,以便将功劳归于原作者。

public class AutoCompleteDocument extends PlainDocument {

    private final List<String> dictionary = new ArrayList<String>(); 
    private final JTextComponent jTextField;

    public AutoCompleteDocument(JTextComponent field, String[] aDictionary) {
        jTextField = field;
        dictionary.addAll(Arrays.asList(aDictionary));
    }

    public void addDictionaryEntry(String item) {
        dictionary.add(item);
    }

    @Override
    public void insertString(int offs, String str, AttributeSet a)
            throws BadLocationException {
        super.insertString(offs, str, a);
        String word = autoComplete(getText(0, getLength()));
        if (word != null) {
            super.insertString(offs + str.length(), word, a);
            jTextField.setCaretPosition(offs + str.length());
            jTextField.moveCaretPosition(getLength());
        }
    }

    public String autoComplete(String text) {
        for (Iterator<String> i = dictionary.iterator(); i.hasNext();) {
            String word = i.next();
            if (word.startsWith(text)) {
                return word.substring(text.length());
            }
        }
        return null;
    }  
}

然后使用它只需用类似的东西初始化它

AutoCompleteDocument autoCompleteDoc;

autoCompleteDoc = new AutoCompleteDocument(aJTextField, anArray);
aJTextField.setDocument(autoCompleteDoc);

希望它会有所帮助

于 2013-02-04T17:38:53.653 回答
0

这是一种可能的实现:

public class Predict
{
    private final static String [] COLORS = new String [] {"red", "orange", "yellow", "green", "cyan", "blue", "violet"};

    public static void main (String [] args)
    {
        final JTextField field = new JTextField ();

        field.getDocument ().addDocumentListener (new DocumentListener()
        {
            @Override
            public void removeUpdate (DocumentEvent e)
            {
                // Do nothing
            }

            @Override
            public void insertUpdate (DocumentEvent e)
            {
                if (e.getOffset () + e.getLength () == e.getDocument ().getLength ())
                    SwingUtilities.invokeLater (new Runnable()
                    {
                        @Override
                        public void run ()
                        {
                            predict (field);
                        }
                    });
            }

            @Override
            public void changedUpdate (DocumentEvent e)
            {
                // Do nothing
            }
        });

        JFrame frame = new JFrame ("Auto complete");
        Container contentPane = frame.getContentPane ();
        contentPane.setLayout (new BorderLayout ());
        contentPane.add (field, BorderLayout.CENTER);
        frame.pack ();
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setVisible (true);
    }

    private static void predict (JTextField field)
    {
        String text = field.getText ();

        String prediction = null;

        for (String color: COLORS)
        {
            if (color.startsWith (text) && !color.equals (text))
            {
                if (prediction != null) return;

                prediction = color;
            }
        }

        if (prediction != null)
        {
            field.setText (prediction);

            field.setCaretPosition (text.length ());
            field.select (text.length (), prediction.length ());
        }
    }
}
于 2013-02-04T17:40:19.240 回答