可编辑的 JComboBox

发布于 2024-11-16 04:59:16 字数 945 浏览 3 评论 0原文

我有可编辑的 JComboBox 并希望从其输入中向其添加值,例如,当我在 JComboBox 中键入内容并按 Enter 键时,我希望该文本出现在 JComboBox< /code> list :

public class Program extends JFrame 
    implements ActionListener {
    private JComboBox box;

    public static void main(String[] args) {
        new Program().setVisible(true);
    }

    public Program() {
        super("Text DEMO");
        setSize(300, 300);
        setLayout(new FlowLayout());
        Container cont = getContentPane();
        box = new JComboBox(new String[] { "First", "Second", "..." });
        box.setEditable(true);
        box.addActionListener(this);
        cont.add(box);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        box.removeActionListener(this);
        box.insertItemAt(box.getSelectedItem(), 0);
        box.addActionListener(this);
    }
}

不幸的是,当我按 Enter 时,插入了两个值而不是一个。

为什么?

I have editable JComboBox and wish to add values to it from its input,e.s when I typing something in JComboBox and press enter I want that text appear in JComboBox list :

public class Program extends JFrame 
    implements ActionListener {
    private JComboBox box;

    public static void main(String[] args) {
        new Program().setVisible(true);
    }

    public Program() {
        super("Text DEMO");
        setSize(300, 300);
        setLayout(new FlowLayout());
        Container cont = getContentPane();
        box = new JComboBox(new String[] { "First", "Second", "..." });
        box.setEditable(true);
        box.addActionListener(this);
        cont.add(box);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        box.removeActionListener(this);
        box.insertItemAt(box.getSelectedItem(), 0);
        box.addActionListener(this);
    }
}

unfortunately when I press enter two values were inserted instead of one.

Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热情消退 2024-11-23 04:59:16

来自 API 用于 JComboBox

做出选择后,ActionListener 将收到 ActionEvent。如果组合框是可编辑的,则编辑停止时将触发 ActionEvent。

因此,您的 ActionListener 被调用两次。

要仅在编辑时将项目添加到 JComboBox,您可以检查正确的 ActionCommand,如下所示:

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("comboBoxEdited")) {
    //code
    }
}

编辑(-> 事件调度线程)

正如 trashgod 已经提到的,您还应该仅在事件调度线程中创建和显示框架:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Program().setVisible(true); 
        }
    });
}

From the API for JComboBox :

The ActionListener will receive an ActionEvent when a selection has been made. If the combo box is editable, then an ActionEvent will be fired when editing has stopped.

Thus, your ActionListener is called two times.

To only add the item to the JComboBox when edited, you can check for the correct ActionCommand like this :

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("comboBoxEdited")) {
    //code
    }
}

edit ( -> event dispatch thread)

As already mentioned by trashgod, you should also create and show your frame only in the event dispatch thread :

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Program().setVisible(true); 
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文