/
MainPanel.java
228 lines (210 loc) · 7.65 KB
/
MainPanel.java
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
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import com.sun.java.swing.plaf.windows.WindowsComboBoxUI;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Objects;
import java.util.stream.IntStream;
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;
public final class MainPanel extends JPanel {
private MainPanel() {
super(new BorderLayout());
// Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
// @Override public void eventDispatched(AWTEvent event) {
// if (event instanceof MouseWheelEvent) {
// Object source = event.getSource();
// if (source instanceof JScrollPane) {
// System.out.println("JScrollPane");
// return;
// }
// ((MouseWheelEvent) event).consume();
// }
// }
// }, AWTEvent.MOUSE_WHEEL_EVENT_MASK);
JComboBox<String> combo1 = new JComboBox<String>(makeModel(5)) {
@Override public void updateUI() {
super.updateUI();
if (getUI() instanceof WindowsComboBoxUI) {
setUI(new WindowsComboBoxUI() {
@Override protected ComboPopup createPopup() {
return new BasicComboPopup2(comboBox);
}
});
} else {
setUI(new BasicComboBoxUI() {
@Override protected ComboPopup createPopup() {
return new BasicComboPopup2(comboBox);
}
});
}
}
};
JComboBox<String> combo2 = new JComboBox<String>(makeModel(20)) {
@Override public void updateUI() {
super.updateUI();
if (getUI() instanceof WindowsComboBoxUI) {
setUI(new WindowsComboBoxUI() {
@Override protected ComboPopup createPopup() {
return new BasicComboPopup3(comboBox);
}
});
} else {
setUI(new BasicComboBoxUI() {
@Override protected ComboPopup createPopup() {
return new BasicComboPopup3(comboBox);
}
});
}
}
};
Box box = Box.createVerticalBox();
box.add(makeTitledPanel("default:", new JComboBox<>(makeModel(5))));
box.add(Box.createVerticalStrut(5));
box.add(makeTitledPanel("default:", new JComboBox<>(makeModel(20))));
box.add(Box.createVerticalStrut(5));
box.add(makeTitledPanel("disable right click in drop-down list:", combo1));
box.add(Box.createVerticalStrut(5));
box.add(makeTitledPanel("disable right click and scroll in drop-down list:", combo2));
box.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
add(box, BorderLayout.NORTH);
setPreferredSize(new Dimension(320, 240));
}
private static Component makeTitledPanel(String title, Component c) {
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createTitledBorder(title));
p.add(c);
return p;
}
private static ComboBoxModel<String> makeModel(int size) {
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
IntStream.range(0, size).forEach(i -> model.addElement("No." + i));
return model;
}
public static void main(String[] args) {
EventQueue.invokeLater(MainPanel::createAndShowGui);
}
private static void createAndShowGui() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException ignored) {
Toolkit.getDefaultToolkit().beep();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
ex.printStackTrace();
return;
}
JFrame frame = new JFrame("@title@");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class BasicComboPopup2 extends BasicComboPopup {
private transient MouseListener handler2;
@Override public void uninstallingUI() {
super.uninstallingUI();
handler2 = null;
}
// Java 8: protected BasicComboPopup2(JComboBox<?> combo) {
// Java 9: protected BasicComboPopup2(JComboBox<Object> combo) {
@SuppressWarnings("unchecked")
protected BasicComboPopup2(JComboBox combo) {
super(combo);
}
@Override protected MouseListener createListMouseListener() {
if (Objects.isNull(handler2)) {
handler2 = new Handler2();
}
return handler2;
}
private final class Handler2 extends MouseAdapter {
@Override public void mouseReleased(MouseEvent e) {
if (!Objects.equals(e.getSource(), list)) {
return;
}
if (list.getModel().getSize() > 0) {
// <ins>
if (!SwingUtilities.isLeftMouseButton(e) || !comboBox.isEnabled()) {
return;
}
// </ins>
// JList mouse listener
if (comboBox.getSelectedIndex() == list.getSelectedIndex()) {
comboBox.getEditor().setItem(list.getSelectedValue());
}
comboBox.setSelectedIndex(list.getSelectedIndex());
}
comboBox.setPopupVisible(false);
// workaround for cancelling an edited item (bug 4530953)
if (comboBox.isEditable() && Objects.nonNull(comboBox.getEditor())) {
comboBox.configureEditor(comboBox.getEditor(), comboBox.getSelectedItem());
}
}
}
}
class BasicComboPopup3 extends BasicComboPopup {
// Java 8: protected BasicComboPopup3(JComboBox<?> combo) {
// Java 9: protected BasicComboPopup3(JComboBox<Object> combo) {
@SuppressWarnings("unchecked")
protected BasicComboPopup3(JComboBox combo) {
super(combo);
}
// // Fixed 8u60: [JDK-8033069] mouse wheel scroll closes combobox popup - Java Bug System
// // https://bugs.openjdk.org/browse/JDK-8033069
// @Override protected JScrollPane createScroller() {
// JScrollPane sp = new JScrollPane(list) {
// @Override protected void processEvent(AWTEvent e) {
// if (e instanceof MouseWheelEvent) {
// JScrollBar toScroll = getVerticalScrollBar();
// if (Objects.isNull(toScroll) || !toScroll.isVisible()) {
// ((MouseWheelEvent) e).consume();
// return;
// }
// }
// super.processEvent(e);
// }
// };
// sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
// sp.setHorizontalScrollBar(null);
// return sp;
// }
// Java 9:
// @Override protected JList<Object> createList() {
// return new JList<>(comboBox.getModel()) {
@SuppressWarnings("unchecked")
@Override protected JList createList() {
return new JList<Object>(comboBox.getModel()) {
@Override protected void processMouseEvent(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
return;
}
MouseEvent ev = e;
if (e.isControlDown()) {
// Fix for 4234053. Filter out the Control Key from the list.
// i.e., don't allow CTRL key deselection.
ev = new MouseEvent(
e.getComponent(), e.getID(), e.getWhen(),
// e.getModifiers() ^ InputEvent.CTRL_MASK,
// Java 10:
// e.getModifiersEx() ^ Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx(),
e.getModifiersEx() ^ InputEvent.CTRL_DOWN_MASK,
e.getX(), e.getY(),
e.getXOnScreen(), e.getYOnScreen(),
e.getClickCount(),
e.isPopupTrigger(),
MouseEvent.NOBUTTON);
}
super.processMouseEvent(ev);
}
};
}
}