We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,328 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Updating JComboBox without refreshing GUI

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?

3
Contributors
2
Replies
5 Hours
Discussion Span
5 Months Ago
Last Updated
3
Views
03hasnam
Junior Poster in Training
70 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

bguild
Posting Whiz
335 posts since Oct 2012
Reputation Points: 167
Solved Threads: 79
Skill Endorsements: 8
DavidKroukamp
Master Poster
Team Colleague
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0701 seconds using 2.86MB