Jump to content

Dropdown for choosing an item


Recommended Posts

Posted (edited)

Let a class extend JFrame in order to let this class be used as a GUI window. Either manually or using a designer, add visual components to an instance of this JFrame class.

 

To add a dropdown box, you would typically use the class called JComboBox. Create one of these and add it to your JFrame (Note that JFrame has a method 'add'). In order to add and retrieve items from this combobox, you should set its backing Model. A common way to do this is creating a new instance of DefaultComboBoxModel, adding elements of your desired type into this model, and then setting this as the model on your combobox instance. Later, you can get the selected item from this model by invoking JComboBox#getSelectedItem.

Edited by FrostBug
  • Like 2
Posted

If you are using enum which is the smart way to do things and also using a jframe then you don't really need a default combo box model. You can just set your combo box to your enum.

Let's say you only want a person to choose a location based off your enum and no other option. I rather tell you to use a joptionpane. It's pretty much a simple pip up box and depending if it's a yes or no question, or a blank field for someone to type in. Or even better a combo box. It includes a cancel and ok button.

It's pretty simple for the basic needs. All you need to do is make the option return the data type you need: string, int, enum variable.

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...