2015-05-28 67 views
0

有没有简单的方法将JComboBox的起始索引设置为“1”或“2”? 如果你开始你的应用程序索引是正常的设置为“0”,但我想从索引“1”开始。JComboBox将起始索引设置为“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())); 
}); 

回答

6

使用JComboBox#setSelectedIndex(int anIndex)

在指数anIndex选择的项目。

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

+0

OP是否需要这个或一个索引的组合框?我还不清楚。 –

+0

我想设置起始值。当我开始我的程序时,有一个函数可以将这些项目添加到JComboBox中,并且不会在开始时设置这些项目。我想设置一个附加项目。该程序是从数组列表中绘制数据,还有一些不同的变量和comboBoxes arte来选择变量。因此,当我启动Programm并且没有变量集时,我看不到这个图,例如我想在启动时设置变量1。 – Dusius