Netbeans JComboBox多列

时间:2014-04-03 13:24:50

标签: java swing netbeans jtable jcombobox

有没有办法在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

谢谢!

2 个答案:

答案 0 :(得分:4)

应该做什么的是将数据存储在Country个对象中,其中包含字段nameiso。我真的没有看到在组合框中显示iso的重点。从您的绘图中,您似乎不希望它显示在初始显示中,为什么在下拉?

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

请参阅此处的示例。注意:该示例仅显示国家/地区名称,但如果您确实也想要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();
            }
        });

    }
}

答案 1 :(得分:0)

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

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