1

I have created 2 comboBoxes.

The 1st comboBox drop down is created based on a radioButton selection.

The 2nd comboBox drop down list is decided based on the selection made in the 1st comboBox.

The first comboBox pulls its list from a column within one of my SQLite tables.

How can I get the the first comboBox's selection (which is the category column in the table) to cross-reference the selection ID (Cat_ID)?

Example table_1:

Cat_ID Category 1. Test1 2. Test2

In the 2nd comboBox, I want to listen for a selection to be made in the 1st comboBox. Then take the 1st comboBox selection ID (Cat_ID) to query my SQLite database for all entries with the matching ID number on a second table and list them in the 2nd comboBox.

Hopefully this makes sense.

Here is the first comboBox code:

radioButton_1.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            try{
                con = DriverManager.getConnection("jdbc:sqlite:WB.db");
                con.setAutoCommit(false);

                stmt = con.createStatement();
                String s = "SELECT * FROM Categories;";
                rs = stmt.executeQuery(s);

                while(rs.next()){
                comboBox.removeItem(rs.getString(2));
                comboBox.addItem(rs.getString(3));
        }
            }
            catch (SQLException e){
                JOptionPane.showMessageDialog(null, "didnt pull from database");
            }finally{
                try{
                    stmt.close();
                    rs.close();
                    con.close();
                }catch (Exception e){
                    JOptionPane.showMessageDialog(null, "ERROR CLOSE");
                }
        }
        }
    });
B.Thompson
  • 27
  • 7
  • First, you completely and absolutely separate your database code from your GUI code. That way, you can test your database code separately from your GUI code. Second, you read all the values you need from your database before you even show the GUI. That way, you can change the values based on user selection quickly, without having to do database reads. – Gilbert Le Blanc Feb 10 '16 at 21:40
  • Using the mousePressed or the mouseReleased methods give you more consistent results than mouseClicked. – Gilbert Le Blanc Feb 10 '16 at 21:48
  • Thanks For the information Gilbert, It completely makes since. I will rearrange my code. – B.Thompson Feb 11 '16 at 23:34
  • Do you have any ideas on how I should write the second comboBox to perform the function I identified in my initial question? – B.Thompson Feb 11 '16 at 23:39
  • [How to control a combo box by using another combo box swing](http://stackoverflow.com/a/10368105/300257) – Gilbert Le Blanc Feb 11 '16 at 23:50
  • Yes, that's exactly what I want to do. – B.Thompson Feb 12 '16 at 00:13

0 Answers0