2013-02-04 51 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()); 
     } 
    } 
}