将jCombobox与MySQL表同步 [英] Synchronize a jCombobox with a MySQL Table

查看:151
本文介绍了将jCombobox与MySQL表同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用NetBeans GUI设计器创建了一个数据库应用程序.

I've created a database application with the NetBeans GUI-Designer.

带有组合框的GUI(绑定到MySQL数据库表用户和团队):

GUI with Comboboxes (Bound to MySQL databasetables user and team):

在按钮上新建-> jDialog-执行查询以在数据库中存储新用户:

on Button new -> jDialog - executes a query to store a new user in database:

问题:组合框在程序启动时更新,但在运行程序时未更新.

Problem: Combobox is updated at the programstart but not while running the program.

问题:保存新用户或团队时,是否可以直接更新组合框中的条目?而我该如何实施呢?

Question: Is it possible to update the entries in my combobox directly when a new user or team is saved? And how could I Implement this?

这是单击JDialog中的saveButton时要做的事情:

Here is what I do when clicking on the saveButton in the JDialog:

int k=st.executeUpdate(
    "INSERT INTO User (username) " + " VALUES ('"+ name + "')");
//Here I'd like to update the jComboBox1 directly if possible
Outerclass.jComboBox1...; 
JOptionPane.showMessageDialog(null, "User is successfully saved");' 

推荐答案

只需更新组件的 sscce .

附录:给出了对JComboBox的引用,

Addendum: Given a reference to a JComboBox,

private final JComboBox combo = new JComboBox();

您可以更新其模型,如下所示.本示例将name添加到列表的开头,但 SortedComboBoxModel 是一个吸引人的选择.

you can update its model, as shown below. This example adds name to the beginning of the list, but SortedComboBoxModel is an appealing alternative.

DefaultComboBoxModel model = (DefaultComboBoxModel) combo.getModel();
model.insertElementAt(name, 0);

附录:更简单地,使用combo本身可用的方法

Addendum: More simply, use the method available to the combo itself,

combo.insertElementAt(name, 0);

这篇关于将jCombobox与MySQL表同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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