Bonjour à tous,
Voilà j'ai un problème un peu particulier...
J'ai testé la création d'un petit composant tout bête mais efficace, c'est une JComboBox à laquelle on ajoute une surcouche qui permet de filtrer les entrées dans la drop down list en fonction de ce que l'utilisateur écrit.
Sachant que s'il écrit une entrée qui n'existe pas, on garde cette valeur quand même (la valeur de la combo peut donc être définie par l'utilisateur).
Voici le code de cette combo dans un petit exemple self-compilable:
Maintenant, mon problème se situe un peu après... Je souhaite intégrer cette JComboBox dans une JTable... Le problème est que soit j'ai un comportement bizarre (vous pourrez en juger rapidement par vous-même), soit j'ai une belle exception que plein de personnes ont eu mais je n'arrive pas à la résoudre.
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 package demo; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.List; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JTextField; public class MyComboDemo { private MyComboDemo() { } public static List<String> populateArray() { final List<String> test = new ArrayList<String>(); test.add(""); test.add("Mountain Flight"); test.add("Mount Climbing"); test.add("Trekking"); test.add("Rafting"); test.add("Jungle Safari"); test.add("Bungie Jumping"); test.add("Para Gliding"); return test; } public static void makeUI() { final JFrame frame = new JFrame("Adventure in Nepal - Combo Filter Test"); final MyFilterComboBox acb = new MyFilterComboBox(populateArray()); frame.getContentPane().add(acb); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { makeUI(); } private static class MyFilterComboBox extends JComboBox<String> { private final List<String> dataList; final private List<String> filteredDataList = new ArrayList<>(); public MyFilterComboBox(List<String> list) { super(); this.dataList = list; for (String str : dataList) { addItem(str); } setEditable(true); final JTextField textfield = (JTextField) this.getEditor().getEditorComponent(); textfield.addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent e) { changeFilter(textfield); } }); } private void changeFilter(JTextField textfield) { final String textEntered = textfield.getText(); filterList(textEntered); updatePopup(textEntered); } public void filterList(final String enteredText) { this.cleanFilteredDataList(); this.fillFilteredDataList(enteredText); } private void cleanFilteredDataList() { filteredDataList.clear(); } private void fillFilteredDataList(final String enteredText) { final int textSize = enteredText.length(); for (int i = 0; i < dataList.size(); i++) { final String itemCurrent = dataList.get(i).toLowerCase(); if (itemCurrent.length() < textSize) { continue; } if (itemCurrent.substring(0, textSize).compareTo(enteredText.toLowerCase()) == 0) { filteredDataList.add(dataList.get(i)); } } } public void updatePopup(final String enteredText) { if (filteredDataList.size() > 0) { this.setModel(new DefaultComboBoxModel(filteredDataList.toArray())); this.setSelectedItem(enteredText); this.showPopup(); } else { this.hidePopup(); } } } }
Voici un petit exemple self compilable de la JComboBox dans une JTable:
Et voici l'exception que j'obtiens:
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 package demo; import java.awt.BorderLayout; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.List; import javax.swing.DefaultCellEditor; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.AbstractTableModel; public class MyTableComboDemo extends JFrame { final static private String[] COLUMN_NAMES = { "Column 1", "Column 2", "Column 3" }; final static private Object[][] DATA_VALUES = { { "Trekking", 234d, 67d }, { "Trekking", 43d, 853d }, { "Trekking", 89.2d, 109d }, { "Trekking", 9033d, 3092d } }; public static List<String> populateArray() { final List<String> test = new ArrayList<String>(); test.add(""); test.add("Mountain Flight"); test.add("Mount Climbing"); test.add("Trekking"); test.add("Rafting"); test.add("Jungle Safari"); test.add("Bungie Jumping"); test.add("Paragliding"); return test; } private final JPanel topPanel; private final TestJTable table; private final JScrollPane scrollPane; public MyTableComboDemo() { super.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setTitle("Simple Table Application"); setSize(300, 200); // Create a panel to hold all other components topPanel = new JPanel(); topPanel.setLayout(new BorderLayout()); getContentPane().add(topPanel); // Create a new table instance table = new TestJTable(); final TestModel model = new TestModel(); model.addRow(new MyLineObject("Rafting", 25.2, 56.3)); model.addRow(new MyLineObject("Trekking", 25.2, 56.3)); model.addRow(new MyLineObject("Paragliding", 25.2, 56.3)); table.setModel(model); // Add the table to a scrolling pane scrollPane = new JScrollPane(table); topPanel.add(scrollPane, BorderLayout.CENTER); } public static void main(String[] args) { final MyTableComboDemo mainFrame = new MyTableComboDemo(); mainFrame.setVisible(true); } private class TestJTable extends JTable { public TestJTable() { final List<String> test = new ArrayList<String>(); test.add(""); test.add("Mountain Flight"); test.add("Mount Climbing"); test.add("Trekking"); test.add("Rafting"); test.add("Jungle Safari"); test.add("Bungie Jumping"); test.add("Para Gliding"); putClientProperty("terminateEditOnFocusLost", Boolean.TRUE); final MyFilterComboBox combo = new MyFilterComboBox(test); combo.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE); final DefaultCellEditor editor = new DefaultCellEditor(combo); setDefaultEditor(String.class, editor); } } public class TestModel extends AbstractTableModel { private final List<MyLineObject> model; public TestModel() { model = new ArrayList<>(); } @Override public boolean isCellEditable(int row, int column) { return column == 0; } @Override public int getColumnCount() { return 3; } @Override public String getColumnName(int column) { return "Column " + (column + 1); } @Override public Object getValueAt(int row, int column) { Object result = null; switch (column) { case 0: result = model.get(row).getName(); break; case 1: result = model.get(row).getPoids(); break; case 2: result = model.get(row).getAge(); break; default: break; } return result; } @Override public Class<?> getColumnClass(int columnIndex) { return columnIndex == 0 ? String.class : Double.class; } @Override public int getRowCount() { return model.size(); } public void addRow(MyLineObject totoToAdd) { model.add(totoToAdd); fireTableDataChanged(); } } public class MyLineObject { private String name; private double age; private double poids; public MyLineObject(String name, double age, double poids) { super(); this.name = name; this.age = age; this.poids = poids; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getAge() { return age; } public void setAge(double age) { this.age = age; } public double getPoids() { return poids; } public void setPoids(double poids) { this.poids = poids; } } private class MyFilterComboBox extends JComboBox<String> { private final List<String> dataList; final private List<String> filteredDataList = new ArrayList<>(); public MyFilterComboBox(List<String> list) { super(); this.dataList = list; for (String str : dataList) { addItem(str); } setEditable(true); final JTextField textfield = (JTextField) this.getEditor().getEditorComponent(); textfield.addKeyListener(new KeyAdapter() { @Override public void keyTyped(KeyEvent e) { super.keyTyped(e); changeFilter(textfield); } }); } private void changeFilter(JTextField textfield) { final String textEntered = textfield.getText(); filterList(textEntered); updatePopup(textEntered); } public void filterList(final String enteredText) { this.cleanFilteredDataList(); this.fillFilteredDataList(enteredText); } private void cleanFilteredDataList() { filteredDataList.clear(); } private void fillFilteredDataList(final String enteredText) { final int textSize = enteredText.length(); for (int i = 0; i < dataList.size(); i++) { final String itemCurrent = dataList.get(i).toLowerCase(); if (itemCurrent.length() < textSize) { continue; } if (itemCurrent.substring(0, textSize).compareTo(enteredText.toLowerCase()) == 0) { filteredDataList.add(dataList.get(i)); } } } public void updatePopup(final String enteredText) { if (filteredDataList.size() > 0) { this.setModel(new DefaultComboBoxModel(filteredDataList.toArray())); // this.setSelectedItem(enteredText); // setSelectedIndex(-1); this.showPopup(); } else { this.hidePopup(); } } } }
Vous avez une petite idé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 Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:2048) at java.awt.Component.getLocationOnScreen(Component.java:2022) at javax.swing.JPopupMenu.show(JPopupMenu.java:942) at javax.swing.plaf.basic.BasicComboPopup.show(BasicComboPopup.java:208) at javax.swing.plaf.basic.BasicComboBoxUI.setPopupVisible(BasicComboBoxUI.java:878) at javax.swing.JComboBox.setPopupVisible(JComboBox.java:818) at javax.swing.JComboBox.showPopup(JComboBox.java:803) at demo.MyTableComboDemo$MyFilterComboBox.updatePopup(MyTableComboDemo.java:262) at demo.MyTableComboDemo$MyFilterComboBox.changeFilter(MyTableComboDemo.java:223) at demo.MyTableComboDemo$MyFilterComboBox.access$0(MyTableComboDemo.java:220) at demo.MyTableComboDemo$MyFilterComboBox$1.keyTyped(MyTableComboDemo.java:215) at java.awt.Component.processKeyEvent(Component.java:6471) at javax.swing.JComponent.processKeyEvent(JComponent.java:2828) at java.awt.Component.processEvent(Component.java:6293) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4872) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4698) at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1887) at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762) at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727) at java.awt.Component.dispatchEventImpl(Component.java:4742) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4698) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:694) at java.awt.EventQueue$3.run(EventQueue.java:692) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:708) at java.awt.EventQueue$4.run(EventQueue.java:706) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Merci d'avance !
Partager