【问题标题】:Netbeans JComboBox multiple columnsNetbeans JComboBox 多列
【发布时间】:2014-04-03 13:24:49
【问题描述】:

有没有办法在 Netbeans 的 JComboBox 中添加两列?

我想做以下事情: 在 cboBox 中,我想用 ISO 代码填充国家列表,例如:

+----------------+---+
| Afghanistan    | V | < The Combo Box :D
+----------------+---+
         ↓↓↓
+----+---------------+
|ISO |  Country      |
+----+---------------+
| AF | Afghanistan   |
| AX | Åland Islands |
| AL | Albania       |
|... | ...           |
+----+---------------+

然后,当用户选择一个国家/地区时,我需要提取 ISO 代码(第 0 列)以存储在配置文件中。稍后应再次读取并显示为国家名称,而不是 ISO 代码。

我一直在寻找解决方案,但我只能找到如何将 cboBox 放入 JTable 中。

(这是我使用/改编的列表:http://www.textfixer.com/resources/dropdowns/country-list-iso-codes.txt

谢谢!

【问题讨论】:

  • 您必须在 JComboBox 中搜索 JTable,但可以使用标准渲染器

标签: java swing netbeans jtable jcombobox


【解决方案1】:

应该将数据存储在Country 对象中,字段为nameiso。我真的没有看到在组合框中显示 iso 的意义。从您的绘图来看,您似乎不希望它显示在初始显示中,那么为什么要显示在下拉列表中?

对于显示,您可以使用DefaultListCellRenderer 并从每个Country 中提取名称值。当您从组合框中选择一个国家/地区时,它已经拥有 ListCountry 对象,因此您可以从选中的 Country 中提取 iso

请参见此处的示例。注意:示例只显示国家名称,但如果你真的想要iso,只需将渲染更改为setText(country.getIso() + " | " + country.getName());

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

import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.SwingUtilities;

public class ComboBoxDemo {

    private List<Country> countries;
    private JComboBox cBox;

    public ComboBoxDemo() {
        countries = createCountryList();
        cBox = createComboBox(countries);

        JFrame frame = new JFrame();
        frame.add(cBox);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JComboBox createComboBox(List<Country> countries) {
        final JComboBox comboBox = new JComboBox(countries.toArray());
        comboBox.setRenderer(new ComboBoxRenderer());
        comboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    Country country = (Country) comboBox.getSelectedItem();
                    System.out.println(country.getIso());
                }
            }
        });
        return comboBox;
    }

    private class ComboBoxRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            JLabel label = (JLabel) super.getListCellRendererComponent(list,
                    value, index, isSelected, cellHasFocus);
            Country country = (Country) value;
            label.setText(country.getName());
            return label;
        }
    }

    private List<Country> createCountryList() {
        List<Country> list = new ArrayList<>();
        list.add(new Country("Afghanistan", "AF"));
        list.add(new Country("Åland Islands", "AX"));
        list.add(new Country("Albania", "AL"));
        return list;
    }

    public class Country {
        private String name;
        private String iso;

        public Country(String name, String iso) {
            this.name = name;
            this.iso = iso;
        }

        public String getName() {
            return name;
        }

        public String getIso() {
            return iso;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ComboBoxDemo();
            }
        });

    }
}

【讨论】:

  • 自定义渲染器会破坏键盘导航。您可能希望通过覆盖Country 中的toString() 来解决此问题,这应该返回与JComboBox 中看到的相同的字符串(= 在渲染器中生成的字符串)。实际上,如果你重写 toString() 方法,你根本不需要渲染器。
  • @Timmos 是的,这些天我的头脑似乎没有想到显而易见的事情;)我会让 OP 阅读您的评论。懒得编辑了。你可以这样做。
  • A custom renderer will break navigation by keyboard. @Timmos,终于有人意识到这个问题了。欢迎来到论坛 :) 看看我的建议。
【解决方案2】:

使用自定义渲染器只是答案的一半。您还需要使用自定义KeySelectionManager,这样您就不会在使用键盘时破坏组合框的默认选择功能。

请参阅Combo Box With Custom Renderer,了解将渲染器和 KeySelectionManager 组合为一个类的简单解决方案。或者,您可以查看博客中的 Combo Box With Hidden Data 链接,了解有关 Timmos 在 Peeskillet 的回答中提出的建议的更多信息。

【讨论】:

    猜你喜欢
    • 2018-01-20
    • 2014-03-13
    • 2018-07-29
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多