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 - JOBS のみをJobテーブルに追加する 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追加したばかりの新しい雇用者名として、選択したアイテムをすぐに取得するにはどうすればよいですか?

ここに画像の説明を入力

4

1 に答える 1

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(..)ダイアログから取得した名前で呼び出します

すなわち:

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 に答える