Skip to content

Commit a55ccd2

Browse files
author
Alisen Chung
committed
8352905: Open some JComboBox bugs 1
Reviewed-by: honkar, psadhukhan
1 parent 33bdc80 commit a55ccd2

File tree

4 files changed

+455
-0
lines changed

4 files changed

+455
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.awt.Robot;
25+
import java.awt.event.ActionListener;
26+
import javax.swing.JComboBox;
27+
import javax.swing.JFrame;
28+
import javax.swing.JLabel;
29+
import javax.swing.JPanel;
30+
import javax.swing.SwingUtilities;
31+
32+
/*
33+
* @test
34+
* @bug 4166593
35+
* @summary Tests that JComboBox fires action events every time the user does an action
36+
* @key headful
37+
* @run main bug4166593
38+
*/
39+
40+
public class bug4166593 {
41+
static JFrame frame;
42+
static JComboBox comboBox;
43+
static volatile int numberOfActionEvents = 0;
44+
45+
public static void main(String[] args) throws Exception {
46+
try {
47+
Robot robot = new Robot();
48+
SwingUtilities.invokeAndWait(() -> createTestUI());
49+
robot.waitForIdle();
50+
robot.delay(250);
51+
52+
// change selected index 3 times
53+
SwingUtilities.invokeAndWait(() -> {
54+
comboBox.setSelectedIndex(1);
55+
comboBox.setSelectedIndex(3);
56+
comboBox.setSelectedIndex(2);
57+
});
58+
robot.waitForIdle();
59+
robot.delay(250);
60+
61+
if (numberOfActionEvents != 3) {
62+
throw new RuntimeException("Unexpected number of Action Events!\n" +
63+
"Expected: 3\nActual: " + numberOfActionEvents);
64+
}
65+
} finally {
66+
SwingUtilities.invokeAndWait(() -> {
67+
if (frame != null) {
68+
frame.dispose();
69+
}
70+
});
71+
}
72+
}
73+
74+
public static void createTestUI() {
75+
comboBox = new JComboBox(new Object[]{
76+
"Bob", "Fred", "Hank", "Joe", "Mildred", "Agatha", "Buffy"
77+
});
78+
JPanel panel = new JPanel();
79+
JLabel label = new JLabel("0");
80+
frame = new JFrame("bug4166593");
81+
comboBox.setEditable(true);
82+
83+
ActionListener actionCounter = e -> {
84+
++numberOfActionEvents;
85+
label.setText(Integer.toString(numberOfActionEvents));
86+
};
87+
88+
comboBox.addActionListener(actionCounter);
89+
90+
panel.add(comboBox);
91+
panel.add(label);
92+
93+
frame.add(panel);
94+
frame.pack();
95+
frame.setLocationRelativeTo(null);
96+
frame.setVisible(true);
97+
}
98+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.awt.Robot;
25+
import javax.swing.DefaultComboBoxModel;
26+
import javax.swing.JComboBox;
27+
import javax.swing.JFrame;
28+
import javax.swing.JLabel;
29+
import javax.swing.JPanel;
30+
import javax.swing.SwingUtilities;
31+
import javax.swing.event.ListDataEvent;
32+
import javax.swing.event.ListDataListener;
33+
34+
/*
35+
* @test
36+
* @bug 4180054
37+
* @summary Tests that DefaultComboBoxModel doesn't fire a "contents changed" unnecessarily
38+
* @key headful
39+
* @run main bug4180054
40+
*/
41+
42+
public class bug4180054 {
43+
static JFrame frame;
44+
static JComboBox comboBox;
45+
static volatile int numberOfContentsChangedEvents = 0;
46+
47+
public static void main(String[] args) throws Exception {
48+
try {
49+
Robot robot = new Robot();
50+
SwingUtilities.invokeAndWait(() -> createTestUI());
51+
robot.waitForIdle();
52+
robot.delay(250);
53+
54+
// change selected index 3 times
55+
SwingUtilities.invokeAndWait(() -> {
56+
comboBox.setSelectedIndex(1);
57+
comboBox.setSelectedIndex(3);
58+
comboBox.setSelectedIndex(2);
59+
comboBox.setSelectedIndex(2);
60+
});
61+
robot.waitForIdle();
62+
robot.delay(250);
63+
64+
if (numberOfContentsChangedEvents != 3) {
65+
throw new RuntimeException("Unexpected number of Contents Changed Events!\n" +
66+
"Expected: 3\nActual: " + numberOfContentsChangedEvents);
67+
}
68+
} finally {
69+
SwingUtilities.invokeAndWait(() -> {
70+
if (frame != null) {
71+
frame.dispose();
72+
}
73+
});
74+
}
75+
}
76+
77+
public static void createTestUI() {
78+
frame = new JFrame("bug4180054");
79+
JPanel panel = new JPanel();
80+
JLabel label = new JLabel("0");
81+
82+
DefaultComboBoxModel model = new DefaultComboBoxModel();
83+
for (int i = 0; i < 100; ++i) {
84+
model.addElement(Integer.toString(i));
85+
}
86+
comboBox = new JComboBox(model);
87+
comboBox.setEditable(true);
88+
89+
ListDataListener contentsCounter = new ListDataListener() {
90+
public void contentsChanged(ListDataEvent e) {
91+
++numberOfContentsChangedEvents;
92+
label.setText(Integer.toString(numberOfContentsChangedEvents));
93+
}
94+
95+
public void intervalAdded(ListDataEvent e) {
96+
}
97+
98+
public void intervalRemoved(ListDataEvent e) {
99+
}
100+
};
101+
102+
comboBox.getModel().addListDataListener(contentsCounter);
103+
104+
panel.add(comboBox);
105+
panel.add(label);
106+
107+
frame.add(panel);
108+
frame.pack();
109+
frame.setLocationRelativeTo(null);
110+
frame.setVisible(true);
111+
}
112+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright (c) 2001, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.awt.Dimension;
25+
import java.awt.FlowLayout;
26+
import java.awt.Point;
27+
import java.awt.Robot;
28+
import java.awt.event.ActionListener;
29+
import java.awt.event.InputEvent;
30+
import java.awt.event.KeyEvent;
31+
import javax.swing.JButton;
32+
import javax.swing.JComboBox;
33+
import javax.swing.JFrame;
34+
import javax.swing.JTextField;
35+
import javax.swing.SwingUtilities;
36+
import javax.swing.event.DocumentEvent;
37+
import javax.swing.event.DocumentListener;
38+
39+
/*
40+
* @test
41+
* @bug 4530952
42+
* @summary Tests that double mouse clicks invoke Event
43+
* @key headful
44+
* @run main bug4530952
45+
*/
46+
47+
public class bug4530952 {
48+
static JFrame frame;
49+
static JButton btnAction;
50+
static JComboBox cmbAction;
51+
static volatile Point loc;
52+
static volatile Dimension btnSize;
53+
54+
private static volatile boolean flag;
55+
56+
public static void main(String[] args) throws Exception {
57+
try {
58+
Robot robot = new Robot();
59+
SwingUtilities.invokeAndWait(() -> createTestUI());
60+
robot.waitForIdle();
61+
robot.delay(1000);
62+
63+
// enter some text in combo box
64+
robot.keyPress(KeyEvent.VK_A);
65+
robot.keyRelease(KeyEvent.VK_A);
66+
robot.keyPress(KeyEvent.VK_A);
67+
robot.keyRelease(KeyEvent.VK_A);
68+
robot.waitForIdle();
69+
robot.delay(250);
70+
71+
// find and click action button
72+
SwingUtilities.invokeAndWait(() -> {
73+
loc = btnAction.getLocationOnScreen();
74+
btnSize = btnAction.getSize();
75+
});
76+
robot.waitForIdle();
77+
robot.delay(250);
78+
79+
robot.mouseMove(loc.x + btnSize.width / 2,
80+
loc.y + btnSize.height / 2);
81+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
82+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
83+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
84+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
85+
robot.waitForIdle();
86+
robot.delay(1000);
87+
88+
if (!flag) {
89+
throw new RuntimeException("Failed: button action was not fired");
90+
}
91+
} finally {
92+
SwingUtilities.invokeAndWait(() -> {
93+
if (frame != null) {
94+
frame.dispose();
95+
}
96+
});
97+
}
98+
}
99+
100+
public static void createTestUI() {
101+
frame = new JFrame("bug4530952");
102+
frame.setLayout(new FlowLayout());
103+
104+
btnAction = new JButton("Action");
105+
cmbAction = new JComboBox();
106+
107+
flag = false;
108+
109+
ActionListener al = e -> flag = true;
110+
DocumentListener dl = new DocumentListener() {
111+
@Override
112+
public void changedUpdate(DocumentEvent evt) {
113+
resetButtons();
114+
}
115+
116+
@Override
117+
public void insertUpdate(DocumentEvent evt) {
118+
resetButtons();
119+
}
120+
121+
@Override
122+
public void removeUpdate(DocumentEvent evt) {
123+
resetButtons();
124+
}
125+
};
126+
127+
// Add an editable combo box
128+
cmbAction.setEditable(true);
129+
frame.add(cmbAction);
130+
131+
btnAction.setEnabled(false);
132+
frame.add(btnAction);
133+
134+
btnAction.addActionListener(al);
135+
((JTextField) cmbAction.getEditor().getEditorComponent()).
136+
getDocument().addDocumentListener(dl);
137+
frame.pack();
138+
frame.setLocationRelativeTo(null);
139+
frame.setVisible(true);
140+
}
141+
142+
public static void resetButtons() {
143+
int length = ((JTextField) cmbAction.getEditor().getEditorComponent()).
144+
getDocument().getLength();
145+
btnAction.setEnabled(length > 0);
146+
}
147+
}

0 commit comments

Comments
 (0)