Bonjour à tous,
Le code ci-dessous vous montre ma classe et donc ma comboboxeditable. Je ne peux pas réduire la taille du block sinon nous ne pourrons pas discuter puisque le problème peut venir de l'ensemble des méthodes.

Pour information, le problème survient quand l'utilisateur tape une lettre dans l'editeur puis change de selection avec la touche bas ou haute des flèches du clavier puis clique sur un item.Le model / la valeur est bien sélectionné mais l'editeur n'est pas rafraîchi. Il est rafraîchi la perte du focus.

Merci de votre aide.Bonne journée.

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/**
 * The filtered document for the combobox. It rejects the characters entered
 * by the user that make the string not matching a string value in the item
 * list.
 */
private class FilteredComboEditorDocument extends PlainDocument implements
    KeyListener, FocusListener, DocumentListener {
 
    /**
     * The serial version UID.
     */
    private static final long serialVersionUID = 1L;
 
    /** The empty text. */
    private static final String EMPTY = "";
 
    /**
     * The parent <code>FilteredComboBox</code>.
     */
    protected EditableTextComboBoxWithQuote mCombo;
 
    /**
     * The flag that checks if an "operation" is in progress. Used in
     * {@link FilteredComboEditorDocument#insertString(int, String, AttributeSet)}
     * and {@link FilteredComboEditorDocument#remove(int, int)} in order to
     * not loop inside and with same super methods.
     */
    protected boolean bmIsOperationInProgress;
 
    /** The flag is active when validation button is clicked. */
    private boolean validationButtonClicked;
 
    /** The flag is active when validation button is clicked. */
    private boolean isItemSelected = false;
 
    private boolean isWritting = false;
 
    /**
     * Constructor that just calls the super constructor and initialize the
     * class members.
     * 
     * @param sCombo The parent comboBox.
     */
    public FilteredComboEditorDocument(
        final EditableTextComboBoxWithQuote sCombo) {
        mCombo = sCombo;
        bmIsOperationInProgress = false;
    }
 
    @Override
    public void changedUpdate(DocumentEvent e) {
    }
 
    /**
     * Ensures that a "valid" value (i.e. : a value that exactly matchs one
     * of the string values in the item list) laid in the editor.
     */
    private void ensureAStringIsSelected() {
        try {
            if (getSelectedIndex() == -1) {
                if (getSelectedItem() != null
                    && !EMPTY.equals(getSelectedItem())) {
                    final BasicComboPopup comboPopup = (BasicComboPopup) mCombo
                        .getUI().getAccessibleChild(mCombo, 0);
                    final String textValue = (String) comboPopup
                        .getList()
                        .getModel()
                        .getElementAt(
                            comboPopup.getList().getSelectionModel()
                                .getAnchorSelectionIndex());
                    if (textValue != null
                        && ((String) getSelectedItem()).toLowerCase()
                            .startsWith(textValue.toLowerCase())) {
                        super.remove(0, getLength());
                        // Test if the resulting string is the beginning
                        // of
                        // one of the items in the list.
                        mCombo.isBeginOfItem(textValue);
                        // Add the resulting text thanks to the super
                        // method.
                        super.insertString(0,
                            ((String) getSelectedItem()).toUpperCase(),
                            null);
                        mCombo.setPopupVisible(false);
                    }
                }
            } else {
                super.remove(0, getLength());
                super.insertString(0,
                    ((String) getSelectedItem()).toUpperCase(), null);
            }
        } catch (final BadLocationException e) {
            LOGGER.log(Level.SEVERE, LogMessages.COMBO_BOX_INSERTION_ERROR,
                e, getName());
        }
    }
 
    /**
     * Ensures that a "valid" value (i.e. : a value that exactly matchs one
     * of the string values in the item list) laid in the editor.
     */
    private void ensureAStringIsWrite() {
        try {
            final String textValue = getText(0, getLength());
            if (!EMPTY.equals(textValue)) {
                remove(0, getLength());
                super.insertString(0, textValue.toUpperCase(), null);
            }
        } catch (final BadLocationException e) {
            LOGGER.log(Level.SEVERE, LogMessages.COMBO_BOX_INSERTION_ERROR,
                e, getName());
        }
    }
 
    /**
     * Does nothing here.
     * 
     * @param sEvent The generated event.
     * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
     */
    @Override
    public void focusGained(final FocusEvent sEvent) {
        // AAW-1987 : after selectting an other componant, the Fault Code
        // combo box in history>Search do not refresh the text
        validationButtonClicked = false;
    }
 
    /**
     * Responds to the event generated when the user presses on the @link
     * KeyEvent#VK_TAB key (i.e. : when the focus is lost).
     * <p>
     * It ensures that a valid value is entered in the @link
     * FilteredComboBox editor.
     * 
     * @param sEvent the generated event.
     * @see FocusListener#focusLost(FocusEvent)
     */
    @Override
    public void focusLost(final FocusEvent sEvent) {
        if (!sEvent.isTemporary()) {
            ensureAStringIsSelected();
            validationButtonClicked = true;
        }
    }
 
    @Override
    public void insertString(final int nsOffset, final String sStr,
        final AttributeSet sSet) throws BadLocationException {
        isWritting = true;
        // Take care that there is no custom insertString or remove in
        // progress.
        if (!bmIsOperationInProgress && !validationButtonClicked) {
            // Prevent from calling other remove/insertString operation.
            bmIsOperationInProgress = true;
            // Build the text that would result of this insertion.
            StringBuffer lWrittenText = new StringBuffer();
            lWrittenText.append(getText(0, nsOffset));
            lWrittenText.append(sStr);
            if (nsOffset <= getLength() - 1) {
                lWrittenText.append(getText(nsOffset, getLength()
                    - nsOffset));
            }
            if (!mCombo.isPopupVisible() && !isItemSelected
                && isDisplayable() && isShowing()) {
                mCombo.setPopupVisible(true);
            }
            // Test if the resulting string is the beginning of one of the
            // items in the list.
            final String lTextToWrite = mCombo.isBeginOfItem(lWrittenText
                .toString());
            if (lTextToWrite != null) {
                // If the popup is not visible, shows it.
                if (!mCombo.isPopupVisible() && !isItemSelected
                    && isDisplayable() && isShowing()) {
                    mCombo.setPopupVisible(true);
                }
                // Delete the text in the document.
                super.remove(0, getLength());
                // Add the resulting text thanks to the super method.
                super.insertString(0, lTextToWrite.toUpperCase(), sSet);
                // Place the caret.
                // mTextField.setCaretPosition(nsOffset + sStr.length());
            } else {
                if (mCombo.isPopupVisible() && isDisplayable()) {
                    mCombo.setPopupVisible(false);
                }
                // Delete the text in the document.
                super.remove(0, getLength());
                // Add the resulting text thanks to the super method.
                super.insertString(0,
                    lWrittenText.toString().toUpperCase(), sSet);
            }
            // Unlock the other insertString remove operation.
            bmIsOperationInProgress = false;
        }
    }
 
    @Override
    public void keyPressed(final KeyEvent sEvent) {
        validationButtonClicked = false;
        if (getLength() == 1) {
            // mCombo.setPopupVisible(false);
        }
        if (sEvent.getKeyCode() == KeyEvent.VK_ENTER && isPopupVisible()) {
            ensureAStringIsSelected();
            validationButtonClicked = true;
        } else if (sEvent.getKeyCode() == KeyEvent.VK_DOWN
            || sEvent.getKeyCode() == KeyEvent.VK_UP) {
            ensureAStringIsWrite();
            validationButtonClicked = true;
        }
    }
 
    /**
     * Does nothing here.
     * 
     * @param sEvent The generated event.
     * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
     */
    @Override
    public void keyReleased(final KeyEvent sEvent) {
        validationButtonClicked = false;
        if (sEvent.getKeyCode() == KeyEvent.VK_DOWN
            || sEvent.getKeyCode() == KeyEvent.VK_UP) {
            ensureAStringIsWrite();
            validationButtonClicked = true;
        }
    }
}
 
/**
 * A stack to keep the isItemSelected true value. Used for setSelectedItem
 * function and setModelList function. Necessary for avoiding the combo box
 * choice pop up display problem.
 */
private class IsItemSelectedStack {
 
    /**
     * The index.
     */
    int index = 0;
 
    /**
     * The text field.
     */
    FilteredComboEditorDocument textField;
 
    /**
     * Constructor.
     * 
     * @param textField the text field
     */
    public IsItemSelectedStack(final FilteredComboEditorDocument textField) {
        this.textField = textField;
    }
 
    /**
     * Disables the text field.
     */
    public void pull() {
        index--;
        if (index <= 0) {
            textField.setItemSelected(false);
        }
    }
 
    /**
     * Enables the text field.
     */
    public void push() {
        index++;
        textField.setItemSelected(true);
    }
}
 
private static final String ALL = "ALL";
 
private static final String BACKLASH_QUOTE = "\"";
 
/** Logger. */
private final static AirmanLogger LOGGER = LogManager
    .getLogger(EditableTextComboBoxWithQuote.class);
 
/** SUID. */
private static final long serialVersionUID = 1036416373871814933L;
 
/**
 * The list of string values.
 */
protected Object[] mList;
 
/**
 * The (default) editor of the @link JComboBox.
 */
protected JTextField mTextField;
 
/** Is selected item stack ?. */
private IsItemSelectedStack isItemSelectedStack;
 
/** The selecting item. */
private boolean selectingItem = false;
 
/** The real value. */
private Object realValue;
 
/** The l filtered combo editor document. */
final FilteredComboEditorDocument lFilteredComboEditorDocument;
 
/**
 * Instantiates a new editable text combo box.
 * 
 * @param name the name
 */
public EditableTextComboBoxWithQuote(final String name) {
    this(new String[0]);
    setName(name);
    setEditable(true);
}
 
public EditableTextComboBoxWithQuote(final String[] sList) {
    mList = sList;
    Arrays.sort(mList);
    mTextField = (JTextField) getEditor().getEditorComponent();
    super.setModel(new DefaultComboBoxModel(mList));
    setEditable(true);
    lFilteredComboEditorDocument = new FilteredComboEditorDocument(this);
    mTextField.setDocument(lFilteredComboEditorDocument);
    mTextField.getDocument().addDocumentListener(
        lFilteredComboEditorDocument);
    // To handle "ENTER" key.
    mTextField.addKeyListener(lFilteredComboEditorDocument);
    // To handle "TAB" key (i.e. : focusLost/Gained).
    mTextField.addFocusListener(lFilteredComboEditorDocument);
}
 
/**
 * Check if this combobox contains the value.
 * 
 * @param value the value
 * @return true, if successful
 */
public boolean containsValue(final Object value) {
    if (mList != null && value != null) {
        for (final Object elt : mList) {
            if (elt.equals(value)) {
                return true;
            }
        }
    }
    return false;
}
 
/**
 * {@inheritDoc}
 */
@Override
public void contentsChanged(final ListDataEvent e) {
    Object oldSelection = selectedItemReminder;
    Object newSelection = dataModel.getSelectedItem();
    if (oldSelection == null || !oldSelection.equals(newSelection)) {
        selectedItemChanged();
        if (!selectingItem) {
            fireActionEvent();
        }
    }
}
 
 
public String isBeginOfItem(final String sCompItem) {
    final String lCompLower = sCompItem.toLowerCase();
    for (int i = 0; i < mList.length; i++) {
        if (mList[i] != null
            && ((String) mList[i]).toLowerCase().startsWith(lCompLower)) {
            super.setSelectedIndex(i);
            return ((String) mList[i]).substring(0, sCompItem.length());
        }
    }
    return null;
}
 
/**
 * Process event.
 */
private void processEvent() {
    ((FilteredComboEditorDocument) mTextField.getDocument())
        .ensureAStringIsSelected();
    ((FilteredComboEditorDocument) mTextField.getDocument())
        .setValidationButtonClicked(true);
    hidePopup();
}
 
/**
 * {@inheritDoc}
 */
@Override
public void processKeyEvent(final KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_TAB) {
        hidePopup();
    }
    super.processKeyEvent(e);
}
 
/**
 * {@inheritDoc}
 */
@Override
public void processMouseEvent(final MouseEvent e) {
    switch (e.getID()) {
    case MouseEvent.MOUSE_CLICKED:
        processEvent();
        break;
    default:
        break;
    }
}
 
 
 
/**
 * {@inheritDoc}
 */
@Override
public void setSelectedItem(final Object anObject) {
    Object oldSelection = selectedItemReminder;
    Object objectToSelect = anObject;
    if (oldSelection == null || !oldSelection.equals(anObject)
        || ALL.equals(oldSelection)) {
        if (anObject != null && !isEditable()) {
            // For non editable combo boxes, an invalid selection
            // will be rejected.
            boolean found = false;
            for (int i = 0; i < dataModel.getSize(); i++) {
                Object element = dataModel.getElementAt(i);
                if (anObject.equals(element)) {
                    found = true;
                    objectToSelect = element;
                    break;
                }
            }
            if (!found) {
                return;
            }
        }
        // Must toggle the state of this flag since this method
        // call may result in ListDataEvents being fired.
        selectingItem = true;
        if (ALL.equals(objectToSelect)) {
            realValue = objectToSelect;
            dataModel.setSelectedItem(objectToSelect);
        } else if (!ALL.equals(objectToSelect)) {
            if (isPopupVisible()) {
                String regexp = "\"(.*)\"";
                Matcher matcher = Pattern.compile(regexp).matcher(
                    objectToSelect.toString());
                if (matcher.find()) {
                    if (matcher.group().toString().length() == objectToSelect
                        .toString().length()) {
                        // if selected item have "" and they surround all
                        // the string
                        realValue = objectToSelect;
                        dataModel.setSelectedItem(objectToSelect);
                    } else {
                        // if selected item have "" but they don't surround
                        // all the string
                        // exemple : CPIOM-B2 « (2TF2) »
                        realValue = objectToSelect;
                        dataModel.setSelectedItem(BACKLASH_QUOTE
                            + realValue.toString() + BACKLASH_QUOTE);
                    }
                } else {
                    // If selected item does not have ""
                    realValue = objectToSelect;
                    dataModel.setSelectedItem(BACKLASH_QUOTE
                        + realValue.toString() + BACKLASH_QUOTE);
                }
            } else {
                for (int i = 0; i < dataModel.getSize(); i++) {
                    Object element = dataModel.getElementAt(i);
                    if (anObject.toString().length() > 1) {
                        if (anObject.toString()
                            .substring(1, anObject.toString().length() - 1)
                            .equals(element)) {
                            objectToSelect = element;
                            dataModel.setSelectedItem(BACKLASH_QUOTE
                                + objectToSelect.toString()
                                + BACKLASH_QUOTE);
                            break;
                        }
                    }
                }
            }
        }
        selectingItem = false;
        if (selectedItemReminder != dataModel.getSelectedItem()) {
            // in case a users implementation of ComboBoxModel
            // doesn't fire a ListDataEvent when the selection
            // changes.
            selectedItemChanged();
        }
    }
}