Salut

Je viens de trouver du code super mais je n'arrive pas à l'utiliser.

voilà le classe JCompletionBox:

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
package com.mkhelif.swing;
 
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;
 
/**
 * <p>JCompletionBox enable auto-completion on a JComboBox</p>
 * 
 * @author Marwan KHELIF
 * @version Version 1.0 - 03/07/2007
 */
public class JCompletionBox extends PlainDocument {
	private static final long serialVersionUID = 1L;
 
    private ComboBoxModel model;
    private JTextComponent editor;
 
    private boolean selecting = false;
 
    /**
     * <p>Enable auto-completion on a JComboBox</p>
     * 
     * @param comboBox - the JComboBox we want to add auto-completion.
     */
    public static void enableAutoCompletion (JComboBox comboBox) {
    	new JCompletionBox (comboBox);
    } // enableAutoCompletion ()
 
    /**
     * <p>Constructor of JCompletionBox</p>
     * @param comboBox the JComboBox we want to add auto-completion
     */
    private JCompletionBox (JComboBox comboBox) {
    	super ();
 
        model = comboBox.getModel ();
        editor = (JTextComponent) comboBox.getEditor ().getEditorComponent ();
 
    	comboBox.setEditable (true);
    	((JTextComponent) comboBox.getEditor ().getEditorComponent ()).setDocument (this);
    } // JCompletionBox ()
 
    /*
     * @see javax.swing.text.AbstractDocument#remove(int, int)
     */
    public void remove (int offs, int len) throws BadLocationException {
        if (selecting) {
        	return;
        }
        super.remove (offs, len);
    } // remove ()
 
    /*
     * @see javax.swing.text.PlainDocument#insertString(int, java.lang.String, javax.swing.text.AttributeSet)
     */
    public void insertString (int offs, String str, AttributeSet a) throws BadLocationException {
        if (selecting) {
        	return;
        }
        super.insertString (offs, str, a);
 
        Object item = lookupItem (this.getText (0, this.getLength ()));
        this.setSelectedItem (item);
		if (item == null) {
			return;
		}
 
		this.setText (item.toString ());
		this.highlightCompletedText (offs + str.length ());
    } // insertString ()
 
    /**
     * <p>Replace the text in JComboBox textfield.</p>
     * 
     * @param text - The text to set on JComboBox textfield.
     * @throws BadLocationException
     */
    private void setText (String text) throws BadLocationException {
        super.remove (0, getLength ());
        super.insertString (0, text, null);
    } // setText ()
 
    /**
     * <p>Highlight from start to end of text</p>
     * 
     * @param start - Start of highlighting.
     */
    private void highlightCompletedText (int start) {
        editor.setSelectionStart (start);
        editor.setSelectionEnd (getLength());
    } // highlightCompletedText ()
 
    /**
     * <p>Select item on JComboBox.</p>
     * 
     * @param item - The item to select.
     */
    private void setSelectedItem (Object item) {
        selecting = true;
        model.setSelectedItem (item);
        selecting = false;
    } // setSelectedItem ()
 
    /**
     * <p>Search in JComboBox items to find one which match the pattern.</p>
     * 
     * @param pattern the pattern we are looking for.
     * @return the matching item or null if no item found.
     */
    private Object lookupItem (String pattern) {
        for (int i = 0, n = model.getSize () ; i < n ; i++) {
            Object currentItem = model.getElementAt (i);
            if (startsWithIgnoreCase (currentItem.toString (), pattern)) {
                return currentItem;
            }
        }
        return null;
    } // lookupItem ()
 
    /**
     * <p>Check if str1 starts with str2 ignoring case.</p>
     * 
     * @param str1
     * @param str2
     * @return true - if str1 starts with str2 ignoring case.
     */
    private boolean startsWithIgnoreCase (String str1, String str2) {
        return str1.toLowerCase ().startsWith (str2.toLowerCase ());
    } // startsWithIgnoreCase ()
} // JCompletionBox
J'ai bien comprise que le constructeur JCompletionBox (JComboBox combo) était privé mais je n'arrive pas à utiliser le méthode :
public static void enableAutoCompletion (JComboBox comboBox)

(je crois que c'est celle là qu'il faut utiliser mais je ne vois pas comment utiliser une méthode qui doit elle même créer sa propre instance)

merci de votre aide
claire