简体   繁体   English

如何在jComboBox中检索绑定到数据库表中的对象并将其存储到另一个表

[英]How to retrieve the object in jComboBox that is binded in database table and store it to another table

I had created a table named "model" and "item" in my database. 我在数据库中创建了一个名为“ model”和“ item”的表。 And created classes Model() and Item(). 并创建了Model()和Item()类。 For table "model", it has the following fields :model_ID and model_description. 对于表“模型”,它具有以下字段:model_ID和model_description。 For table "item" : item_ID, item_description, model_ID. 对于表“ item”:item_ID,item_description,model_ID。

Through binding the table "model" and jComboBox using Netbeans 8.2, I was able to display the model_description in jComboBox. 通过使用Netbeans 8.2绑定表“ model”和jComboBox,我能够在jComboBox中显示model_description。

Now what I want to do is to store the selected item in jComboBox into my "item" table. 现在,我要做的就是将jComboBox中的选定项目存储到“项目”表中。

I have tried this code: 我已经试过这段代码:

Session session = NewHibernateUtil.getSessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Model model = (Model) session.get(Model.class, jComboBox.getSelectedIndex+1);
Item item = new Item();
item.setDescription(description);
item.setModel(model);
session.save(item);
transaction.commit();
session.close();

The problem of this code is that what if I will sort the display of model_description in my jComboBox in ascending/descending order, it will not return the right object. 这段代码的问题是,如果我将jComboBox中的model_description显示按升/降序排序,它将不会返回正确的对象。

Is there any way to store directly as model object the selected item in jComboBox? 有什么方法可以将所选项目直接存储在jComboBox中作为模型对象?

Thank you for help! 谢谢你的帮助!

Through binding the table "model" and jComboBox using Netbeans 8.2, I was able to display the model_description in jComboBox. 通过使用Netbeans 8.2绑定表“ model”和jComboBox,我能够在jComboBox中显示model_description。

I have no idea what the Netbeans binding does, but that doesn't seem to be a good solution to me. 我不知道Netbeans绑定的作用,但这对我来说似乎不是一个好的解决方案。 What if you ever move from the Netbeans platform how will your code work or how will you be able to do this in the future? 如果您从Netbeans平台移走怎么办?您的代码将如何工作?或者将来您将如何做?

Instead I would suggest you can store a custom object in the JComboBox. 相反,我建议您可以在JComboBox中存储自定义对象。 So you can store your Item object in the combo box and then create a custom renderer to display the "description" in the combo box. 因此,您可以将Item对象存储在组合框中,然后创建一个自定义渲染器以在组合框中显示“描述”。

The when you want the seledcted item you just use: 当您只需要选择的项目时:

comboBox.getSelectedItem() 

and you have access to your Item object an all its properties. 您就可以访问Item对象及其所有属性。

A custom renderer would look something like: 自定义渲染器如下所示:

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

        if (value instanceof Foo)
        {
            Foo foo = (Foo)value;
            setText( foo.getDescription() );
        }

        return this;
    }
}

Replace the "Foo" object with your "Item" object. 将“ Foo”对象替换为“ Item”对象。

However, when you use a custom renderer you will break the combo box as you will no longer be able to select the item using the keyboard by typing the first character of the item description. 但是,当您使用自定义渲染器时,您将中断组合框,因为您将不再能够使用键盘通过键入项目描述的第一个字符来选择项目。 See Combo Box With Custom Renderer for more information on this problem and a solution. 有关此问题和解决方案的更多信息,请参见带有自定义渲染器的组合框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM