我有一个以这种方式生成的JComboBox。
private JComboBox generateComboBox() {
JComboBox ddl = new JComboBox();
ArrayList<String> items = null;
if(this.editable)
items = (ArrayList<String>) Registry.getListEditable();
else
items = (ArrayList<String>) Registry.getListNotEditable;
for(String s:items)
ddl.addItem(s);
return ddl;
}
此ComboBox在此处使用该代码显示在JFrame上:
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridheight = 3;
constraints.gridwidth = 1;
constraints.gridx = 0;
constraints.gridy = 0;
this.getRootPane().add(ddl_schemes,constraints);
问题是,&#34;下拉&#34;功能不可见。 ComboBox包含值,我可以使用键盘上的箭头键选择这些值,但是我看不到列表。
有什么建议吗?
谢谢!
答案 0 :(得分:0)
这很有效。所以你的问题可能在任何其他方面。发布更多代码或完整示例。 为什么要将所有内容添加到 rootpane ?将其添加到 contentpane 。
了解更多信息:http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html
package testing;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Arrays;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class JComboBoxMaster extends JFrame {
private JComboBox generateComboBox() {
JComboBox ddl = new JComboBox();
List<String> items = Arrays.asList("one", "two", "three");
for (String s : items) {
ddl.addItem(s);
}
return ddl;
}
public static void main(String[] args) {
JComboBoxMaster jcbm = new JComboBoxMaster();
jcbm.setLayout(new GridBagLayout());
jcbm.setDefaultCloseOperation(EXIT_ON_CLOSE);
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridheight = 3;
constraints.gridwidth = 1;
constraints.gridx = 0;
constraints.gridy = 0;
JComboBox test = jcbm.generateComboBox();
jcbm.getContentPane().add(test, constraints);
jcbm.pack();
jcbm.setVisible(true);
// This works
test.addItem("four");
}
}