JComboBox将起始索引设置为" 1"

时间:2015-05-28 20:50:06

标签: java swing jcombobox

是否有一种简单的方法可以将JComboBox的起始索引设置为" 1"或" 2"? 如果你启动你的应用程序,索引正常设置为" 0"但我想从索引" 1"开始代替。

编辑:

JComboBox variableBox_1 = new JComboBox();
for (int i = 0; i < dataModel.getVariableNames().size(); i++) {
    variableBox_1.addItem(dataModel.getVariableNames().get(i));
}
JPanel comBoxPanel1 = new JPanel(new BorderLayout());
JLabel comBoxLabel1 = new JLabel("X:");
comBoxPanel1.add(variableBox_1, BorderLayout.CENTER);
comBoxPanel1.add(comBoxLabel1, BorderLayout.WEST);
optionPanel.add(comBoxPanel1);
variableBox_1.addActionListener((ActionEvent e) -> {
    sp.setVariableNumberX(variableBox_1.getSelectedIndex());
    hg1.setVariableNumber(variableBox_1.getSelectedIndex());
    sp.setXvariableText(dataModel.getVariableNames().get(variableBox_1.getSelectedIndex()));
});

1 个答案:

答案 0 :(得分:6)

使用JComboBox#setSelectedIndex(int anIndex)

  

选择索引为anIndex的项目。

要选择列表中的项目,请使用JComboBox#setSelectedItem(Object anObject)