在 JComboBox 中添加键入的文本作为此 JComboBox 中的对象
Add typed text in JComboBox as object in this JComboBox
我在 JComboBox 中加载了对象,它们有自己的 "toString" 方法。
此组合框用于选择和返回此组合框上带有 "ActionListener" 的对象。
一切正常,直到我决定通过向此组合框键入文本并使用 "Submit" 按钮提交来添加动态添加新对象的功能。
例如,
我的 class 是:
public class SomeCustomClass {
private int id;
private String name;
public SomeCustomClass(String name){
this.name = name;
}
// getters and setters here
}
当我在组合框中键入文本 "Some test text" 并提交时,我想要一个包含新对象 "SomeCustomClass" 的此框,其中名称 = "Some test text".
变体 1 创建一些从 String 到 SomeCustomClass 的自定义转换方法。可能吗?这是个好主意吗?
变体 2 在触发组合框上的 ActionListener 之前找到一种捕获字符串的方法,使用文本创建一个新的 SomeCustomClass 对象并将其再次推回组合框。但是怎么办?我还没有找到 JComboBox 的方法 getString(getText)。
变体 3 你的想法...
我是 Java 的新手,可能是我错过了什么。
所以,在 10 分钟的测试中,我发现...
如果模型中不存在该值,JComboBox#getSelectedIndex
将 return -1
JComboBox#getSelectedVaue
将 return 一个 String
如果该值在模型中不存在
因此,使用其中之一(或两者),您应该知道该值何时存在于模型中。如果没有,您应该能够创建一个新对象,传入 String
值并将其添加到 JComboBox
(假设您使用的是 DefaultComboBoxModel
)
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
DefaultComboBoxModel<Fruit> fruitModel = new DefaultComboBoxModel<>();
fruitModel.addElement(new Fruit("Apple"));
fruitModel.addElement(new Fruit("Banana"));
fruitModel.addElement(new Fruit("Grapes"));
fruitModel.addElement(new Fruit("Pears"));
JComboBox cb = new JComboBox(fruitModel);
cb.setEditable(true);
add(cb);
cb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int index = cb.getSelectedIndex();
Object value = cb.getSelectedItem();
if (!(value instanceof Fruit)) {
System.out.println(value + " is not a fruit");
cb.addItem(new Fruit(value.toString()));
} else {
System.out.println(value + " is a fruit");
}
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public class Fruit {
private String name;
public Fruit(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return getName();
}
}
}
我在 JComboBox 中加载了对象,它们有自己的 "toString" 方法。 此组合框用于选择和返回此组合框上带有 "ActionListener" 的对象。 一切正常,直到我决定通过向此组合框键入文本并使用 "Submit" 按钮提交来添加动态添加新对象的功能。
例如,
我的 class 是:
public class SomeCustomClass {
private int id;
private String name;
public SomeCustomClass(String name){
this.name = name;
}
// getters and setters here
}
当我在组合框中键入文本 "Some test text" 并提交时,我想要一个包含新对象 "SomeCustomClass" 的此框,其中名称 = "Some test text".
变体 1 创建一些从 String 到 SomeCustomClass 的自定义转换方法。可能吗?这是个好主意吗?
变体 2 在触发组合框上的 ActionListener 之前找到一种捕获字符串的方法,使用文本创建一个新的 SomeCustomClass 对象并将其再次推回组合框。但是怎么办?我还没有找到 JComboBox 的方法 getString(getText)。
变体 3 你的想法...
我是 Java 的新手,可能是我错过了什么。
所以,在 10 分钟的测试中,我发现...
-
如果模型中不存在该值,
JComboBox#getSelectedIndex
将 return-1
JComboBox#getSelectedVaue
将 return 一个String
如果该值在模型中不存在
因此,使用其中之一(或两者),您应该知道该值何时存在于模型中。如果没有,您应该能够创建一个新对象,传入 String
值并将其添加到 JComboBox
(假设您使用的是 DefaultComboBoxModel
)
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
DefaultComboBoxModel<Fruit> fruitModel = new DefaultComboBoxModel<>();
fruitModel.addElement(new Fruit("Apple"));
fruitModel.addElement(new Fruit("Banana"));
fruitModel.addElement(new Fruit("Grapes"));
fruitModel.addElement(new Fruit("Pears"));
JComboBox cb = new JComboBox(fruitModel);
cb.setEditable(true);
add(cb);
cb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int index = cb.getSelectedIndex();
Object value = cb.getSelectedItem();
if (!(value instanceof Fruit)) {
System.out.println(value + " is not a fruit");
cb.addItem(new Fruit(value.toString()));
} else {
System.out.println(value + " is a fruit");
}
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public class Fruit {
private String name;
public Fruit(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return getName();
}
}
}