立即更新到Java中的JCombobox

时间:2013-01-05 12:05:39

标签: java mysql sql swing jcombobox

某人希望向数据库添加新作业。 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)

Class Employer_GUI - 包含一个简单的表单和保存按钮,用于将新的雇主保存到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类 - 由一个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如何立即将所选项目作为新添加的雇主名称?

enter image description here

1 个答案:

答案 0 :(得分:2)

如果我理解你想要添加的新员工是在组合框中选择的内容吗?

获得新员工姓名并将其添加到组合框后,只需使用新员工的姓名拨打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)而不是简单地调用setSelectedItem(name) after the data has been added to组合框。

因此,您可能会看到添加雇主对话框返回一个名称或者有一个方法来获取添加到数据库中的名称。在关闭对话框后的组合框类中,您将使用新条目刷新组合框,通过添加员工对话框获取名称,并使用我们从添加雇主对话框中获得的名称调用JComboBox#setSelectedItem(..)使用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;
    }

}