Dus*_*ius 0 java swing jcombobox
有没有简单的方法将a的起始索引设置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()));
});
Run Code Online (Sandbox Code Playgroud)
用途JComboBox#setSelectedIndex(int anIndex):
选择索引处的项目
anIndex.
要选择列表中的项目,请使用JComboBox#setSelectedItem(Object anObject).