getSelectedItem / JComboBox的索引没有更新

时间:2016-03-04 04:26:42

标签: java swing actionlistener jcombobox

对于作业,我正在尝试创建一个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类还没有完成,它被设置为打印测试形式,但无论我做什么,我只会为我做的任何选择打印“Select Formation”。

我做错了什么或错过了什么?

4 个答案:

答案 0 :(得分:1)

那么,而不是打印永不改变的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

实际选择的值

答案 1 :(得分:1)

您可以使用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来实现。我在此列出了ActionListenerItemListener中的一些差异。

  

<强> ActionListener

     
      当您在组合框中更改元素时,即使元素与之前的元素相同,也会调用
  • ActionListener
  •   
  • 更改元素时,只调用一次动作侦听器actionPerformed方法。
  •   
     

<强> ItemListener

     
      当你在组合框中更改元素并且它与之前的元素不同时,会调用
  • ItemListener
  •   
  • 当您使用ItemListener时,itemStateChanged方法被调用两次,因此您必须检查条件if (e.getStateChange() == ItemEvent.SELECTED) {,否则在您选择新元素时该方法会执行两次。
  •   

答案 2 :(得分:0)

您永远不会更新selected的值以响应ComboBox的更改。

答案 3 :(得分:0)

将这行代码插入到程序中,以便每次代码段运行时jComboBox选项都会刷新。这样,您始终可以获得当前选择的当前jComboBox值:

jComboBox.setName("");

例如,在代码中插入它:

public Dropdown(String selected)
    {
        this.selected = selected;
        jComboBoxcb.setName("");
    }