1.11M Members

Updating JComboBox without refreshing GUI

 
0
 

A person wishes to add a NEW Job to the database. A Combobox list the existing employers already in the database for the new Job to be added against. But if an employer is not present customers have the option click on a button to add an employer. Once added that employer should immediately be displayed in the textfield.

I am trying to achieve the above scenario with my coding and mysql database, but cant think of the logic to do so...

Table Employer

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)

Table Job

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 - Consist of a simple form and save button which saves new EMPLOYERS into Employer table

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 - Consist of a FORM to add JOBS only to Job table

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

        }
    }

How could the JComboBox comboEmployer be immediately have the selected item as the new Employer name just added?

 
0
 

You can call comboEmployer.addItem at any time as long as you are in the event dispatch thread, and I'm sure you are in the event dispatch thread in your SaveEmpButtonActionPerformed method. So feel free to add employers immediately or at any time you feel the need, and you can always use setSelectedItem to choose which employer you want to be selected.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article