Vsal
Vsal

Reputation: 83

clear last selected value in JCombobox on selecting another item

I have 8 comboboxes...1st combobox is client and remaining 7 comboboxes depending on it...so if I select A item from client combobox then in user combo I have to get values related to A only and remaining comboboxes also get values related to A only...but here if I select B from client then items related to A should be clear and got items from B only...so how to clear existing items ?

public void actionPerformed(ActionEvent e)
{
    try
    {
        String query="select distinct `User_Name`,`Purchaser_Name`,`Product1`,`User_boss`,`Purchaser_boss`,`Sales_Engineer`,`Sales_boss`  from Client where Client_Name = ?";
        PreparedStatement pst=conn.prepareStatement(query);
        pst.setString(1, (String) client.getSelectedItem());
        ResultSet rs=pst.executeQuery();

        while(rs.next())
        {

            user.addItem(rs.getString("User_Name"));
            user.setSelectedItem(null);
            purchase.addItem(rs.getString("Purchaser_Name"));

            prod.addItem(rs.getString("Product1"));

            uboss.addItem(rs.getString("User_boss"));

            pboss.addItem(rs.getString("Purchaser_boss"));

            sengg.addItem(rs.getString("Sales_Engineer"));

            sboss.addItem(rs.getString("Sales_boss"));

//          System.out.println(query);

        }
//      System.out.println(query);


    }
    catch(Exception h)
    {
        h.printStackTrace();
    }    
}

Upvotes: 1

Views: 333

Answers (1)

Jay Smith
Jay Smith

Reputation: 2480

You can create reference lastSelectedItem. When you make selection ActionEvent is fired and sent to your ActionListener. In listener call method getSelectedItem of JComboBox to store it in lastSelectedItem.

When you select another item in combobox your listener is fired again and you can use lastSelectedItem reference to locate it within jcombobox and clear it.

Upvotes: 1

Related Questions