gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:55:36 25 4
gpt4 key购买 nike

是否有一种简单的方法可以将 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()));
});

最佳答案

使用JComboBox#setSelectedIndex(int anIndex) :

Selects the item at index anIndex.

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

关于java - JComboBox 将起始索引设置为 "1",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30516844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com