java中的预测文本



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

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

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

我的一些程序中有一个自动完成。不幸的是,我在网上找不到信息。我发布了我的代码,但我不是这个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);

希望它能帮助

这里有一个可能的实现:

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

相关内容

  • 没有找到相关文章

最新更新