如何检索绑定在数据库 table 中的 jComboBox 中的对象并将其存储到另一个 table
How to retrieve the object in jComboBox that is binded in database table and store it to another table
我在我的数据库中创建了一个名为 "model" 和 "item" 的 table。并创建了 类 Model() 和 Item()。
对于 table
"model",它有以下字段:model_ID 和 model_description。对于 table "item" : item_ID, item_description, model_ID.
通过使用 Netbeans 8.2 绑定 table "model" 和 jComboBox,我能够在 jComboBox 中显示 model_description。
现在我要做的是将jComboBox中的选中项存储到我的"item"table中。
我试过这个代码:
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();
这段代码的问题在于,如果我将我的 jComboBox 中 model_description 的显示按 ascending/descending 顺序排序,它不会 return 正确的对象。
有什么方法可以将选中的项目直接存储为jComboBox中的模型对象吗?
感谢您的帮助!
Through binding the table "model" and jComboBox using Netbeans 8.2, I was able to display the model_description in jComboBox.
我不知道 Netbeans 绑定的作用,但这对我来说似乎不是一个好的解决方案。如果您离开 Netbeans 平台,您的代码将如何工作或您将来如何做到这一点会怎样?
相反,我建议您可以将自定义对象存储在 JComboBox 中。因此,您可以将 Item 对象存储在组合框中,然后创建自定义渲染器以在组合框中显示 "description"。
当您想要您刚刚使用的所选项目时:
comboBox.getSelectedItem()
并且您可以访问您的 Item 对象及其所有属性。
自定义渲染器看起来像:
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;
}
}
用您的 "Item" 对象替换 "Foo" 对象。
但是,当您使用自定义呈现器时,您将破坏组合框,因为您将无法再使用键盘通过键入项目描述的第一个字符来 select 项目。有关此问题和解决方案的更多信息,请参阅 Combo Box With Custom Renderer。
我在我的数据库中创建了一个名为 "model" 和 "item" 的 table。并创建了 类 Model() 和 Item()。 对于 table "model",它有以下字段:model_ID 和 model_description。对于 table "item" : item_ID, item_description, model_ID.
通过使用 Netbeans 8.2 绑定 table "model" 和 jComboBox,我能够在 jComboBox 中显示 model_description。
现在我要做的是将jComboBox中的选中项存储到我的"item"table中。
我试过这个代码:
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();
这段代码的问题在于,如果我将我的 jComboBox 中 model_description 的显示按 ascending/descending 顺序排序,它不会 return 正确的对象。
有什么方法可以将选中的项目直接存储为jComboBox中的模型对象吗?
感谢您的帮助!
Through binding the table "model" and jComboBox using Netbeans 8.2, I was able to display the model_description in jComboBox.
我不知道 Netbeans 绑定的作用,但这对我来说似乎不是一个好的解决方案。如果您离开 Netbeans 平台,您的代码将如何工作或您将来如何做到这一点会怎样?
相反,我建议您可以将自定义对象存储在 JComboBox 中。因此,您可以将 Item 对象存储在组合框中,然后创建自定义渲染器以在组合框中显示 "description"。
当您想要您刚刚使用的所选项目时:
comboBox.getSelectedItem()
并且您可以访问您的 Item 对象及其所有属性。
自定义渲染器看起来像:
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;
}
}
用您的 "Item" 对象替换 "Foo" 对象。
但是,当您使用自定义呈现器时,您将破坏组合框,因为您将无法再使用键盘通过键入项目描述的第一个字符来 select 项目。有关此问题和解决方案的更多信息,请参阅 Combo Box With Custom Renderer。