Java 6 JComboBox does not work
Hi All,
My application is using some JComboBoxes to select data. There are 2 frames. The first frame calls the second one. JComboBoxes in the first one work fine but JComboBoxes in the second one do not work, when we click the arrow, it hangs. It works fine with Java 5. The problem happens when I switch to use with Java 6 build 104.
Anyone knows how to fix this problem, please help me.
Thanks so much,
lsen
[440 byte] By [
lsena] at [2007-11-26 17:25:29]

# 5
I have demonstration code of this problem:
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Panel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class Test2 {
private static GridBagConstraints c;
public static void main(String[] args) {
JFrame p = new JFrame();
p.setContentPane(makePanel());
p.pack();
p.setVisible(true);
}
private static Panel makePanel() {
Panel container = new Panel();
container.setLayout(new GridBagLayout());
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
JComboBox cb = new JComboBox(new String[] { "1", "2", "3" });
addComponent(cb, container, 0, 0, 1, 1);
addComponent(b1, container, 0, 1, 1, 1);
addComponent(b2, container, 0, 2, 1, 1);
return container;
}
private static void addComponent(Component addThis, Panel addToThis, int x, int y, int height, int width) {
if (c == null) {
c = new GridBagConstraints();
c.insets = new Insets(4, 4, 4, 4);
}
c.gridx = x;
c.gridy = y;
c.gridheight = height;
c.gridwidth = width;
addToThis.add(addThis, c);
}
}
The problem I am having is that the combo box does not drop down. However, this occurs for me in Java 5 and Java 6.
# 6
interesting, if you don't set the insets it works.
it also works with this changed addComponent:
private static void addComponent(Component addThis, Panel addToThis, int x, int y, int height, int width) {
if (c == null) {
c = new GridBagConstraints();
c.insets = new Insets(4, 4, 4, 4);
}
else
c.insets = new Insets(0,0,0,0);
c.gridx = x;
c.gridy = y;
c.gridheight = height;
c.gridwidth = width;
addToThis.add(addThis, c);
}