gpt4 book ai didi

java - 立即更新到 Java 中的 JCombobox

转载 作者:行者123 更新时间:2023-11-29 02:30:22 25 4
gpt4 key购买 nike

一个人希望向数据库中添加一个新工作。 Combobox 列出了数据库中已有的雇主,以便添加新工作。但是,如果雇主不在场,客户可以选择单击按钮添加雇主。添加后,雇主应立即显示在文本字段中。

我正在尝试用我的编码和 mysql 数据库实现上述场景,但想不出这样做的逻辑......

表雇主

CREATE TABLE "Employer" ("employerID" INTEGER PRIMARY KEY  NOT NULL ,
"name" CHAR,
"industry" CHAR,
"contact1" CHAR,
"contact2" CHAR,
"email" CHAR,
"website" CHAR,
"facts" CHAR,
"phone" VACHAR)

表作业

CREATE TABLE "Job" ("jobID" INTEGER PRIMARY KEY  NOT NULL ,
"employerID" INTEGER,
"title" CHAR,
"description" CHAR,
"type" CHAR,"salary" CHAR,
"benefits" CHAR,
"vacancies" INTEGER,
"closing" CHAR,
"requirement" CHAR,
"placement" BOOL,
"applyTo" CHAR,
"status" CHAR,
"posted" CHAR,
"location" CHAR)

类 Employer_GUI - 由一个简单的表单和保存按钮组成,它将新的 EMPLOYERS 保存到 Employer 表中

private void SaveEmpButtonActionPerformed(java.awt.event.ActionEvent evt) {

try {
String sql = "INSERT INTO Employer (name,industry,contact1,contact2,email,website,facts,phone) VALUES (?,?,?,?,?,?,?,?)";
pst = conn.prepareStatement(sql);

pst.setString(1, txtName.getText());
pst.setString(2, txtInd.getText());
pst.setString(3, txtC1.getText());
pst.setString(4, txtC2.getText());
pst.setString(5, txtEmail.getText());
pst.setString(6, txtWeb.getText());
pst.setString(7, txtFacts.getText());
pst.setString(8, txtPhone.getText());
pst.execute();
JOptionPane.showMessageDialog(null, ""+txtName.getText()+" added to database!");
this.setVisible(false);
}

catch (Exception e) {
JOptionPane.showMessageDialog(null, ""+txtName.getText()+" could not be added!");
}
finally {
try {
rs.close(); pst.close(); }
catch(Exception e) { } }

}

//类 Job_GUI - 包含一个仅向 Job 表添加 JOBS 的 FORM

private void fillCombo() {
try {
String sql = "SELECT * FROM Employer";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();

while(rs.next()) {
String empName = rs.getString("name");
comboEmployer.addItem(empName);

}
}

JComboBox comboEmployer 如何立即将所选项目作为刚刚添加的新雇主名称?

enter image description here

最佳答案

如果我知道您希望添加的新员工成为在组合框中选择的员工?

获得新员工姓名并将其添加到组合框后,只需调用 JComboBox#setSelectedItem(Object o)新员工的名字。

即:

String newEmpName=...;
//code to add new employee goes here
//code to fill combobox with update values goes here
//now we set the selecteditem of the combobox
comboEmployer.setSelectedItem(newEmpName);

更新

根据您的意见:

基础知识:

1) 从添加员工对话框中获取新员工姓名或与组合框中的项目相匹配的任何标识符。

2) 将数据添加到 combobox` 后,只需调用 setSelectedItem(name)。



因此您可能会看到您的添加雇主 对话框返回一个名称或有一个方法来获取已添加到数据库中的名称。在对话框关闭后的组合框类中,您将使用新条目刷新组合框,通过添加员工对话框获取添加的名称,并调用 JComboBox#setSelectedItem(..) 我们从 < em>使用 getter 或静态变量添加雇主对话框



即:



class SomeClass {

JFrame f=...;
JComboBox cb=new ...;

...

public void someMethod() {
AddEmployerDialog addEmpDialog=new AddEmployerDialog(f);//wont return until exited or new name added

String nameAdded=addEmpDialog.getRecentName();//get the name that was added

//clear combobox of all old entries
DefaultComboBoxModel theModel = (DefaultComboBoxModel)cb.getModel();
theModel.removeAllElements();

//refresh combobox with the latest names from db
fillCombo();

//now we set the selected item of combobox with the new name that was added
cb.setSelectedItem(nameAdded);
}

}

class AddEmployerDialog {

private JDialog dialog;
private String empName;//emp name will be assigned when save is pressed or whatever

public AddEmployerDialog(JFrame frame) {

dialog=new JDialog(f);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setModal(true);//so that we dont return control until exited or done
//add components etc
dialog.pack();
dialog.setVisible(true);

}

public String getRecentName() {
return empName;
}

}

关于java - 立即更新到 Java 中的 JCombobox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14171600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com