-
Bug
-
Resolution: Unresolved
-
P4
-
None
-
16
-
x86_64
-
windows_10
A DESCRIPTION OF THE PROBLEM :
If JComboBox.setSelectedItem is called twice before it becomes visible, then on the first popup show its scroll does not contain current selected item.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Run test code
2. Click on the combo box to show the popup
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Current selected item should be visible in popup on the first open.
ACTUAL -
Current selected item is not visible in popup (you have to scroll up to see it).
---------- BEGIN SOURCE ----------
import javax.swing.*;
import java.awt.*;
public class DropDownTest {
public static void main(String[] args) {
String[] months = new String[12];
for (int i = 0; i < 12; i++)
months[i] = String.valueOf(i + 1);
JComboBox ch = new JComboBox(months);
ch.setSelectedItem(months[3]);
ch.setSelectedItem(months[2]);
JFrame frame = new JFrame();
frame.add(ch, BorderLayout.CENTER);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
You can create DefaultComboBoxModel first:
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(months);
model.setSelectedItem(months[3]);
JComboBox<String> ch = new JComboBox<>(model);
ch.setSelectedItem(months[2]);
FREQUENCY : always
If JComboBox.setSelectedItem is called twice before it becomes visible, then on the first popup show its scroll does not contain current selected item.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Run test code
2. Click on the combo box to show the popup
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Current selected item should be visible in popup on the first open.
ACTUAL -
Current selected item is not visible in popup (you have to scroll up to see it).
---------- BEGIN SOURCE ----------
import javax.swing.*;
import java.awt.*;
public class DropDownTest {
public static void main(String[] args) {
String[] months = new String[12];
for (int i = 0; i < 12; i++)
months[i] = String.valueOf(i + 1);
JComboBox ch = new JComboBox(months);
ch.setSelectedItem(months[3]);
ch.setSelectedItem(months[2]);
JFrame frame = new JFrame();
frame.add(ch, BorderLayout.CENTER);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
You can create DefaultComboBoxModel first:
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(months);
model.setSelectedItem(months[3]);
JComboBox<String> ch = new JComboBox<>(model);
ch.setSelectedItem(months[2]);
FREQUENCY : always