JCombobox字符串项(可见)和整数键(固有) [英] JCombobox string item (visible) and integer key (inherent)

查看:91
本文介绍了JCombobox字符串项(可见)和整数键(固有)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库架构=,它将在JCombobox中显示为JTable列以选择名称.但是我想将ID字段插入(作为外键)在另一个表中.

I have a database schema = which will be shown in a JCombobox as a JTable column to select a name. But I want the ID field to insert (as a foreign key) in another table.

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

Usually, selecting an Item in drop down, bring the selected item to the shown area of the combo box.

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

What I want to do is, when select any item (string) in the combo box its corresponding integer key (can be kept in a sorted map) should be shown in the combobox placeholder area, so that when take the value of the JTable.getValueAt(row, column), I get the integer key, not the string item value. Please help me how can I do that?

推荐答案

由于没有自动方法可以实现:(.. 我正在使用地图保留我的值和键.

Since there is no automatic way to do it :(. I am using a Map to keep my values and key.

private TreeMap <Integer, String> categoryMap = new TreeMap<Integer, String>();
private JComboBox comboCategory = new JComboBox();

获取所有类别(键,值)并填充地图和组合

Get all the categories (key, value) and populate the map and combo

private void addComboColumnData() {
        try {
            Class.forName("org.sqlite.JDBC");
            Connection conn = DriverManager.getConnection(conURL);
            Statement stat = conn.createStatement();
            ResultSet rs = stat.executeQuery("SELECT id, name FROM categories");            
            comboCategory.removeAllItems();
            comboCategory.addItem(""); // blank value.
            categoryMap.clear();
            while(rs.next()){
                String name = rs.getString("name");
                comboCategory.addItem(name);
                categoryMap.put(rs.getInt("id"), name);
            }           
            rs.close();
            conn.close();
        } catch (Exception e){
            JOptionPane.showMessageDialog(this, e.toString());
            e.printStackTrace();
        }
    }

我的JTable的第四列应该是组合框,

The fourth column of my JTable should be combobox,

// set the fourth column as combo
            TableColumn categoryColumn = tableData.getColumnModel().getColumn(4);
            categoryColumn.setCellEditor(new DefaultCellEditor(comboCategory));

在组合框中编辑值时,它会显示文本,但是要更新数据库表中的值,我必须获取整数键.

When editing the value in the combobox, it show the text, but for updating the value in the database table I have to get the integer key.

// show Category name in text in the category field.
                    if(columnNames[i].equalsIgnoreCase("category")){
                        Object obj = rs.getObject(i+1);
                        if(obj != null && (obj instanceof Integer) )
                            row[i] = (String) categoryMap.get((Integer) obj);
                    }   

对于数据库更新,

Object value = tableData.getModel().getValueAt(rowIndex, 4);
        int categoryID = 0;

        if(value!= null){
            if (value instanceof String && !((String)value).isEmpty()) {
                categoryID = getKeyForValue((String)value);
            } else if(value instanceof Number) {
                categoryID = (Integer) value;
            }
        }

这是getKeyForValue,

And here is the getKeyForValue,

private int getKeyForValue(String value) {
    for (Entry<Integer, String> entry : categoryMap.entrySet()) {
         if (entry.getValue().equals(value)) {
             return entry.getKey();
         }
     }
    return 0;
}

这篇关于JCombobox字符串项(可见)和整数键(固有)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆