2013-01-05 51 views
2

一個人希望將新作業添加到數據庫中。 A Combobox列出已添加到數據庫中的現有僱主以添加新的工作。但是,如果僱主不在場,客戶可以選擇點擊按鈕添加僱主。一旦添加,僱主應立即顯示在文本框中。立即更新到Java中的JCombobox

我想實現我的編碼和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 - 由一個形成於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); 

     } 
    } 

怎麼能立即剛纔添加JComboBoxcomboEmployer所選擇的項目爲新僱主的名字嗎?

enter image description here

回答

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

UPDATE

按照您的意見:

基礎知識:

1)獲取新員工的名字或任何標識符匹配的項目在你的添加員工對話框的組合框中。

2)比簡單地打電話setSelectedItem(name) after the data has been added to組合框`。

因此,您可能會看到您的添加僱主對話框返回一個名稱或有一個方法來獲取已添加到數據庫中的名稱。在對話框後您的ComboBox類是封閉的,你會刷新新的記錄項組合框,獲得通過添加員工對話框中添加的名稱,並調用JComboBox#setSelectedItem(..)與我們從把名字用干將或靜態變量

添加僱主對話框即:

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

} 
+0

我覺得你的代碼假定'JCombobox'是在同一個班級,它不是其在另一GUI和有數據庫概念爲新僱主會立即添加到數據庫中。 – Hoody

+0

@MuminAli @MuminAli不管重要是什麼邏輯,從那裏你可能必須傳遞變量,使用靜態變量或getter和setter來檢索你需要在組合框中設置選定員工的信息,甚至在組合框內創建一個方法類接受一個字符串,並允許你調用它,它將設置選定的項目作爲該字符串,無論最符合您的需求,但我只是讓我的課,添加新員工返回最新添加的條目的名稱? –

+0

Il嘗試getter和setter,但它會更新而GUI仍然可見?意味着生活。 – Hoody