Reputation: 11
I have a combobox where I need the user to select something before the program can continue further. Is there someway to make sure the user has selected something before proceeding?
Upvotes: 0
Views: 1498
Reputation: 531
There is an interface in Java for this. You can learn how to create one here: ActionListener. This interface handles as I quote "An action event occurs, whenever an action is performed by the user." So in your case, when an user clicks on something before the game can continue you can add something like:
continueGame();
under the actionPerformed method and have more code under that method to handle what you want.
I hope this helps other people as I am 2 years late to this question.
Upvotes: 0
Reputation: 694
I would suggest to disable all related components until the user selects its value. Once the selection has changed, you can then enable those components back.
Upvotes: 3
Reputation: 7076
You can move that part of your code where you "continue further", until after you the user has performed an action on the combobox. See Action Listeners.
Upvotes: 1
Reputation: 285405
Check the JComboBox API and you'll find methods that will help you such as getSelectedItem()
and getSelectedIndex()
. Actually if you checked the API, you wouldn't have even needed to ask this question here. It's a great resource that you will want to get into the habit of using.
Upvotes: 2