gpt4 book ai didi

java - 添加 JComboBox 中键入的文本作为此 JComboBox 中的对象

转载 作者:行者123 更新时间:2023-12-01 10:03:47 27 4
gpt4 key购买 nike

我已经加载了对象的 JComboBox,这些对象有自己的“toString”方法。该组合框用于选择并通过该组合框上的“ActionListener”返回该对象。一切工作正常,直到我决定添加通过在此组合框中键入文本并使用“提交”按钮提交来动态添加新对象的功能。

例如,

我的类(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”的此框,其中 name =“Some test text”。

变体 1 创建一些从 String 到 SomeCustomClass 的自定义转换方法。是否可以?这是个好主意吗?

变体 2 找到一种方法在触发组合框上的 ActionListener 之前捕获字符串,使用文本创建一个新的 SomeCustomClass 对象,然后再次将其推回组合框。但如何呢?我还没有找到 JComboBox 的 getString(getText) 方法。

变体 3 您的想法...

我是 Java 新手,可能我错过了一些东西。

最佳答案

所以,在 10 分钟的测试中我发现......

  • JComboBox#getSelectedIndex 如果模型中不存在该值,将返回 -1
  • JComboBox#getSelectedVaue 如果模型中不存在该值,将返回 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();
}

}

}

关于java - 添加 JComboBox 中键入的文本作为此 JComboBox 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36610937/

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