'How to track selected option from Combo Box 1 and show corresponding option on combo box 2 in java
i haven't touched java for almost 6 years just trying to finish this side project, i'm trying to make the jComboBox2 to show specific company positions stored in the database based from the selected company from jComboBox1, i know ActionListener just couldn't get it right, i just have to make it dynamic that whenever a different company is selected from combo1 the corresponding company positions should show on combo2, here's sample code:
public class NewMessage extends javax.swing.JFrame{
/**
* Creates new form NewMessage
*/
public NewMessage() {
initComponents();
showCompany();
showPosition();
}
public void showCompany(){
try {
String jdbcUrl = "jdbc:sqlite:/client.db";
Connection connection = DriverManager.getConnection(jdbcUrl);
String query = "SELECT * FROM contacts";
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(query);
while(result.next()){
String company = result.getString("company");
jComboBox1.addItem(company);
}
}catch(SQLException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
public void showPosition(){
try {
String jdbcUrl = "jdbc:sqlite:/client.db";
Connection connection = DriverManager.getConnection(jdbcUrl);
String companyName = jComboBox1.getSelectedItem().toString();
String query = "SELECT * FROM contacts WHERE company='"+companyName+"' ";
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(query);
while(result.next()){
String positions = result.getString("position");
jComboBox2.addItem(positions);
}
}catch(SQLException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
