从字符串设置选定项 JComboBox
Set Selected Item JComboBox from String
我有一个 JComboBox
,它的值由两部分组成 int-String,如下所示:
01-one
02-two
03-three
所以现在我只有 String
部分,并且想 setSelectedItem
这个部分的项目,但我认为这是不可能的,因为值不匹配或不相同
myComboBox.setSelectedItem("?" + myString);
所以我想做的是:
myComboBox.setSelectedItem("like myString");
有人想设置 select 与组合框中的值相似的项目,或者这是不可能的?
如果您希望能够 select 完全按照您在描述中指出的字符串,我会创建一个 class 来表示您的特定项目,并在其中维护int 和 string 作为单独的字段,并将 toString()
覆盖为 return 您想要的表示形式。然后,您可以使用一种方法仅根据字符串搜索您的项目。对于相对较少的项目,这是高效且简单的。如果您有大量项目,我建议使用字符串作为键将它们作为值存储在 HashMap
中。
import java.awt.BorderLayout;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JFrame;
class Item {
int intValue;
String strValue;
public Item(int intValue, String strValue) {
this.intValue = intValue;
this.strValue = strValue;
}
public String toString() {
return intValue + " - " + strValue;
}
}
public class TestCombo {
private static JComboBox<Item> cb;
public static void main(String[]args) {
JFrame f = new JFrame();
f.setSize(640,400);
cb = new JComboBox<>();
cb.addItem(new Item(1, "one"));
cb.addItem(new Item(2, "two"));
cb.addItem(new Item(3, "three"));
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(cb, BorderLayout.NORTH);
f.setVisible(true);
selectItemByString("three");
}
private static void selectItemByString(String s) {
for (int i=0; i<cb.getItemCount(); i++) {
if (cb.getItemAt(i).strValue.equals(s)) {
cb.setSelectedIndex(i);
break;
}
}
return;
}
}
您可以尝试使用 .contains
方法,查看组合框中的第一项是否包含该特定单词并重复它直到找到特定索引。
例如:
if (jComboBox1.getItemAt(0).toString ().contains ("two"))
{
jComboBox1.setSelectedIndex(0);
}
然后重复该步骤或尝试使用 for 循环,如果您的组合框包含很多项目,那就太好了。
我有一个 JComboBox
,它的值由两部分组成 int-String,如下所示:
01-one
02-two
03-three
所以现在我只有 String
部分,并且想 setSelectedItem
这个部分的项目,但我认为这是不可能的,因为值不匹配或不相同
myComboBox.setSelectedItem("?" + myString);
所以我想做的是:
myComboBox.setSelectedItem("like myString");
有人想设置 select 与组合框中的值相似的项目,或者这是不可能的?
如果您希望能够 select 完全按照您在描述中指出的字符串,我会创建一个 class 来表示您的特定项目,并在其中维护int 和 string 作为单独的字段,并将 toString()
覆盖为 return 您想要的表示形式。然后,您可以使用一种方法仅根据字符串搜索您的项目。对于相对较少的项目,这是高效且简单的。如果您有大量项目,我建议使用字符串作为键将它们作为值存储在 HashMap
中。
import java.awt.BorderLayout;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JFrame;
class Item {
int intValue;
String strValue;
public Item(int intValue, String strValue) {
this.intValue = intValue;
this.strValue = strValue;
}
public String toString() {
return intValue + " - " + strValue;
}
}
public class TestCombo {
private static JComboBox<Item> cb;
public static void main(String[]args) {
JFrame f = new JFrame();
f.setSize(640,400);
cb = new JComboBox<>();
cb.addItem(new Item(1, "one"));
cb.addItem(new Item(2, "two"));
cb.addItem(new Item(3, "three"));
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(cb, BorderLayout.NORTH);
f.setVisible(true);
selectItemByString("three");
}
private static void selectItemByString(String s) {
for (int i=0; i<cb.getItemCount(); i++) {
if (cb.getItemAt(i).strValue.equals(s)) {
cb.setSelectedIndex(i);
break;
}
}
return;
}
}
您可以尝试使用 .contains
方法,查看组合框中的第一项是否包含该特定单词并重复它直到找到特定索引。
例如:
if (jComboBox1.getItemAt(0).toString ().contains ("two"))
{
jComboBox1.setSelectedIndex(0);
}
然后重复该步骤或尝试使用 for 循环,如果您的组合框包含很多项目,那就太好了。