2015-05-28 68 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