JComboBox 项目更改

发布于 2024-11-16 15:10:21 字数 362 浏览 4 评论 0原文

我的 JComboBox 模型包含诸如 item1item2item1 之类的项目。我的问题是,当我在 JComboBox 中选择第三个项目 (item1) 并检查 getSelectedIndex() 时,它总是返回 0。

如果该项目与我的模型如何以不同的方式获得每个项目的索引?例如:

  • item1 返回 0
  • item 2 返回 1
  • item1 返回 2

My JComboBox model contains item like item1, item2, item1. My problem is when I select third item (item1) in JComboBox and check getSelectedIndex() it always returns 0.

If the item is same in my model how can I get index of each item differently? Like:

  • item1 returns 0
  • item 2 returns 1
  • item1 returns 2

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

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

发布评论

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

评论(3

仅此而已 2024-11-23 15:10:21

它返回 index = 0。因为 getSelectedIndex() 方法对 JComboBox 中的对象使用 .equals 并将其与选定的对象进行比较。在您的情况下,因为 item1 也在索引 0 处,所以它发现条件为 true 并返回 0。如果您想获得不同的索引,则必须重写 getSelectedIndex() 方法。

JComboBox 默认 getSelectedIndex() 方法的概述位于 Java2s

public int getSelectedIndex() {
        Object sObject = dataModel.getSelectedItem();
        int i, c;
        Object obj;

        for (i = 0, c = dataModel.getSize(); i < c; i++) {
            obj = dataModel.getElementAt(i);
            if (obj != null && obj.equals(sObject))
                return i;
        }
        return -1;
    }

您应该在 2 个条目中获得不同的内容 [如果项目对象有名称或其他任何内容,则可能是 itemName]期望的结果。重写 getSelectedIndex() 并比较所有不同的内容。如果两个条目完全相同,那么添加两次有什么意义呢?

It returns index = 0. Because the method getSelectedIndex() use .equals on objects that are in the JComboBox and compare it with the selected one. In your case because item1 is also at index 0 it finds the condition true and returns 0. If you want to get different index then you have to override the getSelectedIndex() method.

An outline of default getSelectedIndex() method of JComboBox found at Java2s:

public int getSelectedIndex() {
        Object sObject = dataModel.getSelectedItem();
        int i, c;
        Object obj;

        for (i = 0, c = dataModel.getSize(); i < c; i++) {
            obj = dataModel.getElementAt(i);
            if (obj != null && obj.equals(sObject))
                return i;
        }
        return -1;
    }

You should have something [may be itemName if item object has a name or anything else] different in 2 entries to get desired result. Override getSelectedIndex() and compare the thing that is meant to be differ in all. If both entries are completely same then whats the point of adding it twice?

再浓的妆也掩不了殇 2024-11-23 15:10:21

如果 JComboBox 中的两个条目对应于同一对象,那么即使您单击项目 3,所选的实际项目也将是该对象的第一个条目(即索引最低的条目)
我认为这不适用于相同的对象。

If two entries in the JComboBox correspond to the same Object, then even if you click item 3 the actual item that is selected will be the first entry of that object (i.e. the one with the lowest index)
I don't think that this will work for the same objects.

风吹雪碎 2024-11-23 15:10:21

JList 对于相同的项目没有问题。

工作列表

import javax.swing.event.*;
import javax.swing.*;

class TestList {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                final String[] items = {"item1", "item2", "item1"};
                final JList list = new JList(items);
                final JTextField output = new JTextField(15);
                JPanel gui = new JPanel();
                gui.add(list);
                gui.add(output);
                list.addListSelectionListener(new ListSelectionListener(){
                    public void valueChanged(ListSelectionEvent lse) {
                        int index = list.getSelectedIndex();
                        String outputText =
                            "Index: " +
                            index +
                            "  Value: " +
                            items[index];
                        output.setText(outputText);

                    }
                });
                JOptionPane.showMessageDialog(null, gui);
            }
        });
    }
}

A JList has no problems with identical items.

Working list

import javax.swing.event.*;
import javax.swing.*;

class TestList {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                final String[] items = {"item1", "item2", "item1"};
                final JList list = new JList(items);
                final JTextField output = new JTextField(15);
                JPanel gui = new JPanel();
                gui.add(list);
                gui.add(output);
                list.addListSelectionListener(new ListSelectionListener(){
                    public void valueChanged(ListSelectionEvent lse) {
                        int index = list.getSelectedIndex();
                        String outputText =
                            "Index: " +
                            index +
                            "  Value: " +
                            items[index];
                        output.setText(outputText);

                    }
                });
                JOptionPane.showMessageDialog(null, gui);
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文