user1610075
user1610075

Reputation: 1641

Update jpanel contents

I have a JPanel "presentation" with a JComboBox. This JComboBox takes elements from a database. I have another JPanel "insert" in which I insert database elements.

If I insert a new database element, I'd like "presentation" JPanel combobox updates with the newly inserted element. Is this possible?

Upvotes: 0

Views: 286

Answers (2)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51565

Yes. Use a model to populate your presentation JComboBox. Populate the model from the database to start your process.

When you insert a new database element, you also insert that value into your presentation JComboBox model. Updating the model will update the JComboBox.

Here's an example that would load a ComboBoxModel from a database.

ResultSet results = aJBDCStatement.executeQuery(
        "Select columnName FROM tableName");
DefaultComboBoxModel model = new DefaultComboBoxModel();
while (result.next()) {
    model.addElement(results.getString(1));
}
JComboBox comboBox = new JComboBox(model);

Then later, you would just

model.addElement(elementString);

Upvotes: 2

Harmeet Singh
Harmeet Singh

Reputation: 2616

I would suggest you to use a model and presentation model would be a good start, have a look here

Upvotes: 0

Related Questions