0

Possible Duplicate:
Dynamic JComboBoxes

I am a newbie in java program. I have this problem with my program about comboboxes. I have 3 combobox (cbxType, cbxItem, and cbxColor). I want that the item list in the second combobox (cbxItem) is changed based on the first one (type), and then the third combobox's(cbxColor) item list changed based on the selected item in the second one (cbxItem). i've been trying to solve this problem by my own code, the second combobox work fine when the first one changed, but then the third combobox won't show any item after i change. here is my code. thanks for your help guys and sorry for my bad english..

    private void viewCbxType(){
    String sql;


try {
    sql ="Select distinct productItem from Product ";
    if(cbxType.getSelectedItem() != "<<Product Type>>"){

        String prType = cbxType.getSelectedItem().toString();

        sql ="Select distinct productItem from Product WHERE productType='" +prType+"'";



            cbxItem.removeAllItem();
            cbxItem.setSelectedIndex(0);
        }
    }


    PreparedStatement st = conn.prepareStatement(sql);
    ResultSet rs =st.executeQuery();



    while (rs.next()) {
        String prItem = rs.getString("productItem");

        cbxItem.addItem(prItem);

    }
}catch (SQLException se) {}
}

I call that method at actionPerformed for my first combobox and make similiar to the second

Community
  • 1
  • 1
  • 2
    This has been asked and answered many times on this site, including this thread: [Dynamic JComboBoxes](http://stackoverflow.com/questions/3191837/dynamic-jcomboboxes), and this thread: [changing elements of a jcombobox...](http://stackoverflow.com/questions/5336711/changing-elements-of-a-jcombobox-according-to-the-selection-from-another-jcombob). Voting to close this question as a duplicate. – Hovercraft Full Of Eels Nov 28 '12 at 17:56

1 Answers1

0

You can implements an action listener on the combo box:

public class ComboBoxDemo ... implements ActionListener {
    . . .
        petList.addActionListener(this) {
    . . .
    public void actionPerformed(ActionEvent e) {
        JComboBox cb = (JComboBox)e.getSource();
        String petName = (String)cb.getSelectedItem();
        updateLabel(petName);
    }
    . . .
}

This action listener gets the newly selected item from the combo box, uses it to compute the name of an image file, and updates a label to display the image. The combo box fires an action event when the user selects an item from the combo box's menu. See How to Write an Action Listener, for general information about implementing action listeners:

http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

This could be help you too

http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html

matheuslf
  • 309
  • 1
  • 9