Reputation: 207
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());
}
}
Upvotes: 0
Views: 76
Reputation: 347184
I don't know why you'd think that an ActionListener
was unsuitable for this task. If you're not interested in knowing when an item becomes "unselected", then an ActionListener
is perfect, as you only need to deal with the change of selection.
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
public class TestPane extends JPanel {
private JComboBox<String> typesComboBox;
private JComboBox<String> pokemonComboBox;
private Map<String, List<String>> mapPokemon;
public TestPane() throws IOException {
setBorder(new EmptyBorder(32, 32, 32, 32));
buildDatabase();
DefaultComboBoxModel<String> typesModel = new DefaultComboBoxModel<>();
for (String key : mapPokemon.keySet()) {
typesModel.addElement(key);
}
typesComboBox = new JComboBox<>(typesModel);
typesComboBox.setSelectedItem(null);
pokemonComboBox = new JComboBox<>();
pokemonComboBox.setEnabled(false);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(4, 4, 4, 4);
gbc.anchor = GridBagConstraints.LINE_START;
add(new JLabel("Types: "), gbc);
gbc.gridy++;
add(new JLabel("Pokemon: "), gbc);
gbc.gridx++;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(typesComboBox, gbc);
gbc.gridy++;
add(pokemonComboBox, gbc);
typesComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DefaultComboBoxModel<String> pokemon = new DefaultComboBoxModel<>();
String selected = (String)typesComboBox.getSelectedItem();
if (selected != null) {
List<String> values = mapPokemon.get(selected);
if (values != null) {
pokemon.addAll(values);
}
}
pokemonComboBox.setModel(pokemon);
pokemonComboBox.setEnabled(pokemon.getSize() != 0);
}
});
}
protected void buildDatabase() throws IOException {
mapPokemon = new HashMap<>();
mapPokemon.put("Water", getWaterTypes());
mapPokemon.put("Fire", getFireTypes());
mapPokemon.put("Ground", getGroundTypes());
mapPokemon.put("Rock", getRockTypes());
}
protected List<String> getWaterTypes() throws IOException {
return readFromResource("/resources/WaterTypes.txt");
}
protected List<String> getFireTypes() throws IOException {
return readFromResource("/resources/FireTypes.txt");
}
protected List<String> getGroundTypes() throws IOException {
return readFromResource("/resources/GroundTypes.txt");
}
protected List<String> getRockTypes() throws IOException {
return readFromResource("/resources/RockTypes.txt");
}
protected List<String> readFromResource(String name) throws IOException {
try (BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(name)))) {
return readFrom(br);
}
}
protected List<String> readFrom(BufferedReader br) throws IOException {
List<String> values = new ArrayList<>(32);
String value = null;
while ((value = br.readLine()) != null) {
if (!value.isBlank()) {
values.add(value);
}
}
return values;
}
}
}
Upvotes: 2