Je n'arrive pas à comprendre la mécanique de fonctionnement de ce couple (BasicComboBox avec PlainDocument). Mon but est de créer une combo éditable qui contienne des valeurs de kilométrage formatées par exemple comme '100.000 km par an'. Lorsque on saisit une nouvelle valeur dans la combo, 14200 par ex., au moment où on quitte la combo la valeur se transforme en '14.200 km par an'.
J'ai crée les classes suivantes:
- MyComboBox extends JComboBox
- EditableKilometrageRenderer extends BasicComboBoxRenderer
- EditableComboEditor extends BasicComboBoxEditor
- BoundDocument extends PlainDocument
Je ne vais citer que l'essentiel du code. Ma classe de base MyComboBox:
Voici la création de la combo:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 public class MyComboBox extends JComboBox { String initialText; public P2ComboBox(Object[] ob, String initialText) { super(ob); setSelectedIndex(-1); setRequestFocusEnabled(true); this.initialText = initialText; } }
La classe 'Editor':
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 String[] array = { "10000", "15000", "20000", "25000", "30000", "35000", "40000", "45000", "50000", "50000", "55000", "60000" }; MyComboBox cbBox = new MyComboBox(array, ""); cbBox.setEditable(true); cbBox.setRenderer(new EditableKilometrageRenderer()); EditableComboEditor cbEditor = new EditableComboEditor("choisissez le kilométrage"); cbBox.setEditor(cbEditor);
La classe de 'Document':
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 class EditableComboEditor extends BasicComboBoxEditor { protected int key = -1; protected String initialText; EditableComboEditor(String initialText) { this.initialText = initialText; editor.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { key = e.getKeyCode(); return; } }); } public void setItem(Object o) { if (o == null || o.toString().isEmpty()) { return; } editor.setDocument(new BoundDocument(30)); if (o != null && !o.toString().equals(initialText)) { try { editor.setText((String) o + " km PAR an"); } catch (Exception e) { Log.getLog().info("Exception in setItem:" + e.getMessage()); } } return; } }
Et enfin le renderer:
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 public class BoundDocument extends PlainDocument { protected int maxLength = 0; public BoundDocument(int maxLength) { this.maxLength = maxLength; } public int getMaxLenght() { return maxLength; } public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (str == null) { return; } int afterLength = getLength() + str.length(); if (afterLength > maxLength) { Toolkit.getDefaultToolkit().beep(); return; } super.insertString(offs, str, a); return; } }
Je n'arrive pas à setter la valeur affiché par défault comme 'Choisissez le kilométrage'; En plus si je saisis une valeur en le tapant dans la combo et je la quitte, j'obtiens 2 fois 'km par an' ajouté à la valeur saisie: 14200 km par an km par an'. Quelqu'un peut m'expliquer comment les choses se passent, qui est-ce que est setté et au quel moment? Merci.
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 class EditableKilometrageRenderer extends BasicComboBoxRenderer { public EditableKilometrageRenderer() { } public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { if (index == -1) { setOpaque(false); } else { setOpaque(true); } if ((index != -1) && isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(list.getBackground()); setForeground(list.getForeground()); } setFont(list.getFont()); if (value instanceof Icon) { setIcon((Icon) value); } else { setForeground(Color.gray); setBackground(list.getBackground()); setText(value + " km par an"); } return this; } }
Partager