erenjwz Posted September 19, 2015 Share Posted September 19, 2015 Hi guys, Can somebody tell me how GUI and Jframe works, i wanna get a dropdown where somebody can select an item and the code gets the value that has been selected. Quote Link to comment Share on other sites More sharing options...
FrostBug Posted September 19, 2015 Share Posted September 19, 2015 (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 September 19, 2015 by FrostBug 2 Quote Link to comment Share on other sites More sharing options...
erenjwz Posted September 19, 2015 Author Share Posted September 19, 2015 Thanks for your help, but when i extend it, it says that the JFrame cannot be resolved to a type. (srry still a noob in these things ) Quote Link to comment Share on other sites More sharing options...
Joseph Posted September 19, 2015 Share Posted September 19, 2015 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. 1 Quote Link to comment Share on other sites More sharing options...
erenjwz Posted September 19, 2015 Author Share Posted September 19, 2015 thanks for the tip, could you give me an example of joptionpane? Quote Link to comment Share on other sites More sharing options...
Joseph Posted September 19, 2015 Share Posted September 19, 2015 thanks for the tip, could you give me an example of joptionpane? Enjoy young blood, read the answer.https://www.google.com/url?sa=t&source=web&rct=j&url=http://stackoverflow.com/questions/21957696/how-to-use-joptionpane-with-many-options-java&ved=0CB8QFjABahUKEwifira6ioTIAhUI1YAKHQ1iC-g&usg=AFQjCNFkmJuwSFn2_NzajU6CIp4vTbGfrg&sig2=l9gAukIL1-Hbazld66ryNA Quote Link to comment Share on other sites More sharing options...