[英]getSelectedItem/Index for JComboBox not updating
对于一项任务,我试图创建一个Fantasy Football应用; 并且我正在尝试根据JComboBox中选择的内容更改格式。 但是,无论我进行哪种选择,它始终停留在第一个索引上,并且无论我做什么都不会更新为较新的选择。
我有两个类,Fantasy和Dropdown(JComboBox的ActionListener),并且由于声明需要使用两个单独的类,因此无法将它们合并为一个类。
public class Fantasy extends JFrame
{
String[] formationoptions = {"Select Formation", "4-4-2", "4-3-3", "3-5-2", "5-3-2", "3-4-3", "4-5-1"};
JComboBox<String> formation = new JComboBox<String>(formationoptions);
public Fantasy()
{
super("Fantasy Football");
this.setLayout(new BorderLayout());
this.setSize(400, 600);
this.add(formation, BorderLayout.NORTH);
formation.setSize(400, 25);
this.setVisible(true);
formation.addActionListener(new Dropdown((String) formation.getSelectedItem()));
}
}
Dropdown.java
public class Dropdown implements ActionListener
{
public String selected;
public String a = "Select Formation";
public String b = "4-4-2";
public String c = "4-3-3";
public String d = "3-5-2";
public String e = "5-3-2";
public String f = "3-4-3";
public String g = "4-5-1";
@Override
public void actionPerformed(ActionEvent e)
{
if (selected.equals(a))
{
System.out.println(a);
}
if (selected.equals(b))
{
System.out.println(b);
}
if (selected.equals(c))
{
System.out.println(c);
}
if (selected.equals(d))
{
System.out.println(d);
}
if (selected.equals(e))
{
System.out.println(e);
}
if (selected.equals(f))
{
System.out.println(f);
}
if (selected.equals(g))
{
System.out.println(g);
}
}
public Dropdown(String selected)
{
this.selected = selected;
}
目前,Dropdown类还没有完成,它设置为打印构造以进行测试,但是无论我做什么,我所做的任何选择都只会打印“选择构造”。
我在做什么错还是想念?
所以,与其然后打印selected
它永远不会改变,你需要检查JComboBox
本身,并要求它选择的值是什么...
public class Dropdown implements ActionListener {
public String selected;
@Override
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
System.out.println(cb.getSelectedItem());
}
public Dropdown(String selected) {
this.selected = selected;
}
}
然后,您可以将selected
值更新为JComboBox
实际选择的值
您可以使用ItemListener
实现此目的。
formation.addItemListener(new Dropdown());
并以此更改Dropdown
的类定义
public class Dropdown implements ItemListener {
public String a = "Select Formation";
public String b = "4-4-2";
public String c = "4-3-3";
public String d = "3-5-2";
public String e = "5-3-2";
public String f = "3-4-3";
public String g = "4-5-1";
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
String selected = ((JComboBox)e.getSource())
.getSelectedItem().toString();
System.out.println(selected);
// write here if else ladder or switch case
}
}
}
会的
更新
这也可以通过ActionListener
实现。 我在这里列出了ActionListener
和ItemListener
一些区别。
ActionListener
- 当您更改组合框中的元素时,即使元素与以前的元素相同,也会调用
ActionListener
。- 使用动作侦听器,在更改元素时,只会调用一次
actionPerformed
方法。
ItemListener
- 当您更改组合框中的元素时,
ItemListener
会被调用,它与以前的元素不同。- 当您使用
ItemListener
,itemStateChanged
方法被调用两次,因此您必须检查条件if (e.getStateChange() == ItemEvent.SELECTED) {
,否则,当您选择新元素时该方法执行两次。
响应ComboBox的更改,您永远不会更新selected
的值。
将这行代码插入您的程序,以便每次运行该部分代码时刷新jComboBox选择。 这样,您将始终获得当前选择的当前jComboBox值:
jComboBox.setName("");
例如,将其插入代码中:
public Dropdown(String selected)
{
this.selected = selected;
jComboBoxcb.setName("");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.