2

一个人希望向数据库添加一个新工作。列出数据库中已有的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) { } }  

}

//Class Job_GUI - 由一个 FORM 组成,仅将 JOBS 添加到Job表中

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立即将所选项目作为刚刚添加的新雇主名称?

在此处输入图像描述

4

1 回答 1

2

如果我理解您希望添加的新员工成为组合框中选择的内容?

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

IE:

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)比简单地调用setSelectedItem(name) after the data has been added to组合框`。

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

IE:

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;
    }

}
于 2013-01-05T12:17:01.060 回答