java - JCombobox 字符串项(可见)和整数键(固有)

标签 java jtable jcombobox

我有一个数据库模式 = 它将作为 JTable 列显示在 JCombobox 中以选择名称。但我希望将 ID 字段插入(作为外键)到另一个表中。

通常,在下拉列表中选择一个项目,将所选项目带到组合框的显示区域。

我想要做的是,当在组合框中选择任何项目(字符串)时,其对应的整数键(可以保存在排序映射中)应该显示在组合框占位符区域中,以便在取值时JTable.getValueAt(row, column),我得到整数键,而不是字符串项值。 请帮助我,我该怎么做?

最佳答案

您应该在包含要显示的字符串值和键的整数值的 TableModel 中存储一个对象。然后你访问 table.getValueAt(...) 你可以访问包含这两条信息的对象。

这是一个独立组合框的示例:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class ComboBoxItem extends JFrame implements ActionListener
{
    public ComboBoxItem()
    {
        Vector model = new Vector();
        model.addElement( new Item(1, "car" ) );
        model.addElement( new Item(2, "plane" ) );
        model.addElement( new Item(3, "train" ) );
        model.addElement( new Item(4, "boat" ) );
        model.addElement( new Item(5, "boat aadf asfsdf a asd asd" ) );

        JComboBox comboBox;

        //  Easiest approach is to just override toString() method
        //  of the Item class

        comboBox = new JComboBox( model );
        comboBox.addActionListener( this );
        getContentPane().add(comboBox, BorderLayout.NORTH );

        //  Most flexible approach is to create a custom render
        //  to diplay the Item data

        comboBox = new JComboBox( model );
        comboBox.setRenderer( new ItemRenderer() );
        comboBox.addActionListener( this );
        getContentPane().add(comboBox, BorderLayout.SOUTH );
    }

    public void actionPerformed(ActionEvent e)
    {
        JComboBox comboBox = (JComboBox)e.getSource();
        Item item = (Item)comboBox.getSelectedItem();
        System.out.println( item.getId() + " : " + item.getDescription() );
    }

    class ItemRenderer extends BasicComboBoxRenderer
    {
        public Component getListCellRendererComponent(
            JList list, Object value, int index,
            boolean isSelected, boolean cellHasFocus)
        {
            super.getListCellRendererComponent(list, value, index,
                isSelected, cellHasFocus);

            if (value != null)
            {
                Item item = (Item)value;
                setText( item.getDescription().toUpperCase() );
            }

            if (index == -1)
            {
                Item item = (Item)value;
                setText( "" + item.getId() );
            }


            return this;
        }
    }

    class Item
    {
        private int id;
        private String description;

        public Item(int id, String description)
        {
            this.id = id;
            this.description = description;
        }

        public int getId()
        {
            return id;
        }

        public String getDescription()
        {
            return description;
        }

        public String toString()
        {
            return description;
        }
    }

    public static void main(String[] args)
    {
        JFrame frame = new ComboBoxItem();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
     }

}

关于java - JCombobox 字符串项(可见)和整数键(固有),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4822928/

相关文章:

java - 按代表时间的 2 列对 JTable 进行排序

java - JTable 中 JComboBox 的单元格渲染器类,构造函数中不带参数

java - 如何获取 JComboBox 弹出菜单列表作为组件对象

java - 将数组哈希为整数

java - 单击“下一步”后无法更改 jScrollPane 中的图像

java - 如何为 Java 8 LocalDateTime 编写自定义序列化器

java - Android:构建字符串引用

java - 通过树形图和 SQL 填充 JTable

java - 我怎样才能使组合框的列表更宽?

java - 自动从 JTable 中的特定列获取所有 ID(即不必选择行)