Jump to content

Dropdown for choosing an item


erenjwz

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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