Bonjour à tous,

j'aurai besoin de votre car je fais face un petit problème qui me fait m'arracher les cheveux, j'ai peur de devenir chauve

En effet, j'ai crée une interface graphique Swing dans laquelle se trouve un Jtree et un JTexteArea. Mon problème se trouve dans la fonction d'usage que j'essaye de réaliser. En effet, à l'aide d'une fonction de complétion, je cherche à faciliter la saisie dans le JTextArea pour l'utilisateur.

Ainsi j'ai créé la classe suivante :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
 
package pckg.view;
 
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
 
public class MyDocumentListener implements DocumentListener { 
 
    private static String COMMIT_ACTION = "commit";
    private static enum Mode {INSERT, COMPLETION};
    private Mode mode = Mode.INSERT;
 
    private final ArrayList<String> words;
	private JTextArea EqArea;
	private String content;
	private String prefix;
	private int pos;
 
	public MyDocumentListener(JTextArea EqArea )
 
		this.EqArea=EqArea;
 
        words = new ArrayList<String>(5);
        words.add("spark");
        words.add("special");
        words.add("spectacles");
        words.add("spectacular");
        words.add("swing");
 
        InputMap im = EqArea.getInputMap();
        ActionMap am = EqArea.getActionMap();
        im.put(KeyStroke.getKeyStroke("ENTER"), COMMIT_ACTION);
        am.put(COMMIT_ACTION, new CommitAction());
 
	}
 
	public void insertUpdate(DocumentEvent ev) { 
 
        if (ev.getLength() != 1){
            return;
        }      
 
        content = null;
        pos = ev.getOffset();
        try {
            content = EqArea.getText(0, pos + 1);
            System.out.print("Content : " +content+"\n");
        } 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;
            }
        }
        if (pos - w < 2) {
            // Too few chars
            return;
        }
 
        prefix = content.substring(w + 1).toLowerCase();
        int n = Collections.binarySearch(words, prefix);
        if (n < 0 && -n <= words.size()) {
            String match = words.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, EqArea));
            }
        } else {
            // Nothing found
            mode = Mode.INSERT;
        }
 
    } 
 
 
	 private class CompletionTask implements Runnable {
 
		    String completion;
	        int position;
			JTextArea EqArea;
 
	        CompletionTask(String completion, int position, JTextArea EqArea) {
	            this.completion = completion;
	            this.position = position;
	            this.EqArea=EqArea;
	        }
 
	        public void run() {
	            EqArea.insert(completion, position);
	            EqArea.setCaretPosition(position + completion.length());
	            EqArea.moveCaretPosition(position);
	            mode = Mode.COMPLETION;
	        }
	    }
 
	    private class CommitAction extends AbstractAction {
	        public void actionPerformed(ActionEvent ev) {
	            if (mode == Mode.COMPLETION) {
	                int pos = EqArea.getSelectionEnd();
	                EqArea.insert(" ", pos);
	                EqArea.setCaretPosition(pos + 1);
	                mode = Mode.INSERT;
	            } else {
	                EqArea.replaceSelection("\n");
	            }
	        }
	    }
 
 
    public void removeUpdate(DocumentEvent e) { 
        updateLog(e, "removed from"); 
    } 
    public void changedUpdate(DocumentEvent e) { 
        //Plain text components don't fire these events 
    }
 
    public void updateLog(DocumentEvent e, String action) { 
    } 
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
EqArea.getDocument().addDocumentListener(new MyDocumentListener(EqArea /*, words*/));
Avec :

- EqArea le nom de mon JTextArea.
- words l'arrayList qui contient la liste des mots repérés par la fonction de complétion


Le code ci-dessus marche parfaitement.
Le soucis est le suivant : J'ai un ArrayList<String> qui contient tous les noeud de mon JTree. J'ai mis en commentaire la liste words

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
words = new ArrayList<String>(5);
 
        words.add("spark");
        words.add("special");
        words.add("spectacles");
        words.add("spectacular");
        words.add("swing");
afin de la remplacer par celle dont j'ai besoin. Ainsi, j'ai rajouté à mon constructeur le paramètre suivant :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
 
public MyDocumentListener(JTextArea EqArea , List<String> words)
 
this.words=words;
this.EqArea=EqArea;
Code : Sélectionner tout - Visualiser dans une fenêtre à part
	EqArea.getDocument().addDocumentListener(new MyDocumentListener(EqArea  words));
Mais je ne sais pas pourquoi, je n'arrive pas à réaliser ma complétion....


Merci d'avance pour votre aide