2013-02-04 22 views
0

可能重复:
How to list suggestions to when typing inside the text field文字预测

有没有什么办法让预想中的JTextField中的Java Swing?就像我想从用户那里得到城市的名字一样,那么它应该预测城市。

+2

我想你的意思汽车 - 完整的,没有预测性的文字... –

+0

是的,自动完成。 –

+0

此链接可能有助于http://www.coderanch.com/t/486422/GUI/java/Auto-Complete-JTextField –

回答

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

初始化它希望这将有助于

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