JComboBox检查Select不是来自程序

时间:2012-01-18 11:08:14

标签: java swing actionlistener

我有以下程序

package utests;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Foo extends JFrame {
    private JComboBox combo;

    public static void main(String... args) {
    Foo f = new Foo();
    f.showUI();
    }

    public void showUI() {
    this.setVisible(true);
    JPanel pane = new JPanel();
    this.setContentPane(pane);
    combo = new JComboBox();
    combo.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent arg0) {
        if (arg0.getStateChange() == ItemEvent.SELECTED) {
            System.out.println("You selected " + combo.getSelectedItem());
        }
        }
    });
    refreshBox();

    pane.add(combo);
    this.pack();
    }

    public void refreshBox(){
    combo.removeAllItems();
ArrayList<String> list = new ArrayList<String>();
    list.add("godsf");
    list.add("södlkf");
    list.add("ldsjlkfdsj");

    for (String s : list) {
        combo.addItem(s);
    }
    }
}

该程序将立即显示字符串

You selected godsf

如何检查用户是否检查了某些内容而不是程序?

编辑:我改变了程序:问题是我需要刷新盒子一些时间。每次我刷新框中的数据时,都会触发ItemListener,结果会出错。 如何检查操作是由用户完成而不是由程序本身完成?添加一些布尔似乎不是很像Java ......

3 个答案:

答案 0 :(得分:1)

将方法refreshBox的代码更改为以下代码:

public void refreshBox()
{
    combo.removeAllItems();
    ArrayList<String> list = new ArrayList<String>();
    list.add("godsf");
    list.add("södlkf");
    list.add("ldsjlkfdsj");

    for (String s : list) 
    {
        combo.insertItemAt(s, 0);
    }
}

它符合您的要求吗? 我只是改变了代码:

combo.addItem(s);

代码:

combo.insertItemAt(s, 0);

答案 1 :(得分:0)

我认为有一种方法可以知道用户是否选择了一个选项,而无需编写一行代码。 但是如果你想知道用户是否选择了一个选项,或者你的程序选择了它,那么我认为你必须为此编写你自己的自定义逻辑。

这里的代码只有在用户选择它时才会打印所选选项。

package utests;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Foo extends JFrame {
private JComboBox combo;

public static void main(String... args) {
Foo f = new Foo();
f.showUI();
}

public void showUI() {
this.setVisible(true);
JPanel pane = new JPanel();
this.setContentPane(pane);
combo = new JComboBox();
refreshBox();

pane.add(combo);
combo.addItemListener(new ItemListener() {
    @Override
    public void itemStateChanged(ItemEvent arg0) {
    if (arg0.getStateChange() == ItemEvent.SELECTED) {
        System.out.println("You selected " + combo.getSelectedItem());
    }
    }
});
this.pack();
}

public void refreshBox(){
combo.removeAllItems();
ArrayList<String> list = new ArrayList<String>();
list.add("godsf");
list.add("södlkf");
list.add("ldsjlkfdsj");

for (String s : list) {
    combo.addItem(s);
}
}
}

这里我们已经将ItemListener添加到组合框中,当它已经显示给用户时。因此,只有在用户选择了它时才会打印所选值

答案 2 :(得分:-1)

package utests;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Foo extends JFrame
{
    public static void main(String... args)
    {
        Foo f = new Foo();
        f.showUI();
    }
    bool first = false;
    public void showUI()
    {
        this.setVisible(true);
        JPanel pane = new JPanel();
        this.setContentPane(pane);
        final JComboBox combo = new JComboBox();
        combo.addItemListener(new ItemListener()
        {
            @Override
            public void itemStateChanged(ItemEvent arg0)
            {
                if(first)
                if(arg0.getStateChange() == ItemEvent.SELECTED)
                {
                    System.out.println("You selected " + combo.getSelectedItem());

                }
                //from now on we'll see the selections
                first = true;

            }
        });
        ArrayList<String> list = new ArrayList<String>();
        list.add("godsf");
        list.add("södlkf");
        list.add("ldsjlkfdsj");
        for(String s : list)
        {
            combo.addItem(s);
        }
        pane.add(combo);
        this.pack();
    }
}