gpt4 book ai didi

java - 获取和设置选定的索引 JSpinner

转载 作者:行者123 更新时间:2023-12-04 05:34:38 25 4
gpt4 key购买 nike

我正在使用 JAVA 开发 i18n 应用程序。
我正在使用 JSpinnerString[]作为模型以允许用户选择某些选项。
我的问题是 JSpinner 中的文本正在因语言而变化。
所以我不想使用所选选项的索引而不是它的值。
如何在 JSpinner 中获取(和设置)选定的索引?

最佳答案

您可以获得 JSpinner 的值,然后遍历它的数据(无论您将其设置为什么 List),然后找到它的索引。要设置索引,只需在您想要的索引处获取对象,然后将对象设置为该索引。请参阅下面的示例。您也可以制作自己的 SpinnerModel

搜索示例:

public int getSelectedIndex(JSpinner spinner, List<?> values) {
int index=0;
for(Object o :values) {
if(o.equals(spinner.getValue()))
return index;
index++;
}
return -1;
}
public void setSelectedIndex(JSpinner spinner, List<?> values, int index) {
spinner.setValue(values.get(index));
}

关于java - 获取和设置选定的索引 JSpinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12115871/

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