Partage
  • Partager sur Facebook
  • Partager sur Twitter

JTextField auto complèté

    25 mai 2015 à 19:28:34

    Salu je cherchais sur le  net un code de jtextfield autocomplèté,j'en ai trouver ça marche mais y'a un souci,c'est cela ne marche que pour les petits caractères;voila le code
    package com.example.myapp.console;
    
    import java.awt.event.ActionEvent;
    import java.util.Collections;
    import java.util.List;
    
    import javax.swing.AbstractAction;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    import javax.swing.text.BadLocationException;
    
    public class Autocomplete implements DocumentListener {
    
      private static enum Mode {
        INSERT,
        COMPLETION
      };
    
      private JTextField textField;
      private final List<String> keywords;
      private Mode mode = Mode.INSERT;
    
      public Autocomplete(JTextField textField, List<String> keywords) {
        this.textField = textField;
        this.keywords = keywords;
        Collections.sort(keywords);
      }
    
      @Override
      public void changedUpdate(DocumentEvent ev) { }
    
      @Override
      public void removeUpdate(DocumentEvent ev) { }
    
      @Override
      public void insertUpdate(DocumentEvent ev) {
        if (ev.getLength() != 1)
          return;
    
        int pos = ev.getOffset();
        String content = null;
        try {
          content = textField.getText(0, pos + 1);
        } catch (BadLocationException e) {
          e.printStackTrace();
        }
    
        // Find where the word starts
        int w;
        for (w = pos; w >= 0; w--) {
          if (!Character.isLetter(content.charAt(w))) {
            break;
          }
        }
    
        // Too few chars
        if (pos - w < 2)
          return;
    
        String prefix = content.substring(w + 1).toLowerCase();
        int n = Collections.binarySearch(keywords, prefix);
        if (n < 0 && -n <= keywords.size()) {
          String match = keywords.get(-n - 1);
          if (match.startsWith(prefix)) {
            // A completion is found
            String completion = match.substring(pos - w);
            // We cannot modify Document from within notification,
            // so we submit a task that does the change later
            SwingUtilities.invokeLater(new CompletionTask(completion, pos + 1));
          }
        } else {
          // Nothing found
          mode = Mode.INSERT;
        }
      }
    
      public class CommitAction extends AbstractAction {
        /**
         * 
         */
        private static final long serialVersionUID = 5794543109646743416L;
    
        @Override
        public void actionPerformed(ActionEvent ev) {
          if (mode == Mode.COMPLETION) {
            int pos = textField.getSelectionEnd();
            StringBuffer sb = new StringBuffer(textField.getText());
            sb.insert(pos, " ");
            textField.setText(sb.toString());
            textField.setCaretPosition(pos + 1);
            mode = Mode.INSERT;
          } else {
            textField.replaceSelection("\t");
          }
        }
      }
    
      private class CompletionTask implements Runnable {
        private String completion;
        private int position;
    
        CompletionTask(String completion, int position) {
          this.completion = completion;
          this.position = position;
        }
    
        public void run() {
          StringBuffer sb = new StringBuffer(textField.getText());
          sb.insert(position, completion);
          textField.setText(sb.toString());
          textField.setCaretPosition(position + completion.length());
          textField.moveCaretPosition(position);
          mode = Mode.COMPLETION;
        }
      }
    
    }
     
    Ce qui s'utilise comme suit
    private static final String COMMIT_ACTION = "commit";
    JTextField mainTextField = new JTextField();
    
    // Without this, cursor always leaves text field
    mainTextField.setFocusTraversalKeysEnabled(false);
    ...
    // Our words to complete
    keywords = new ArrayList<String>(5);
            keywords.add("example");
            keywords.add("autocomplete");
            keywords.add("stackabuse");
            keywords.add("java");
    Autocomplete autoComplete = new Autocomplete(mainTextField, keywords);
    mainTextField.getDocument().addDocumentListener(autoComplete);
    
    // Maps the tab key to the commit action, which finishes the autocomplete
    // when given a suggestion
    mainTextField.getInputMap().put(KeyStroke.getKeyStroke("TAB"), COMMIT_ACTION);
    mainTextField.getActionMap().put(COMMIT_ACTION, autoComplete.new CommitAction());
    Alors quand on change les string du ArrayList en grand caractère,brszzzzz ça dort..essayez le code et aidez moi 
    • Partager sur Facebook
    • Partager sur Twitter
      25 mai 2015 à 19:44:28

      Qu'est ce que tu entends par grand et petit caractère? Majuscule et minuscule?
      • Partager sur Facebook
      • Partager sur Twitter
        26 mai 2015 à 18:09:19

        oui les majuscules ne machent pas aidez moi
        • Partager sur Facebook
        • Partager sur Twitter
          26 mai 2015 à 19:18:42

          Il faut que tu enlèves le toLowerCase ligne 62. Mais du coup, si tu recherches mets une minuscule, ca ne te proposera pas ton mot en majuscule.
          • Partager sur Facebook
          • Partager sur Twitter
            26 mai 2015 à 20:31:01

            alors il n'y a pas d'autre solutions?? pour que les deux marche??.. merci ..tu peux me passer ton profil facebook:D
            • Partager sur Facebook
            • Partager sur Twitter

            JTextField auto complèté

            × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
            × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
            • Editeur
            • Markdown