Skip to content
Advertisement

I want the city selected in the first combo box not to appear in the second

If I choose a city FROM: JComboBox then it shouldn’t appear in TO: combo box. How can I code the ActionListener of this action listener?

Screenshot

Code:

JavaScript

Advertisement

Answer

I reworked your code to get away from static. You only need one list of cities for your from and to JComboBoxes.

You use an ActionListener to listen for changes in the from JComboBox, and remove the city from the to JComboBox. We remove the city by removing all the cities and adding them back, one at a time. This allows the user to change his mind about the from city.

Here’s the complete runnable code.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement