Results 1 to 9 of 9
Thread: JComboBox Info
- 09-15-2011, 12:50 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 76
- Rep Power
- 0
JComboBox Info
I am writing an application that the user takes the amount of items they want from a comboBox. I then want to parse/convert back the content to an integer the problem that i am having is that content i am getting is of type Object and i don't know how to convert it to int. Can some one direct me with what to do.
Ok, i'm using a JComboBox component that I pass in a array of type string. which looks like this
What i want is that when the user selects on item a event is generated that gets the value the user selected so i can use it to do some calculations. The problem i am having is that i use the .getSelectedItem () method that returns on Object type which i don't know how to covert to an integer. So can u tell me how to convert it or how to get the data a different wayJava Code:private String [] amountOfTicket = {"0","1","2","3","4","5","6","7","8","9","10"}; private JComboBox adult; public void someFunction () { adult = new JComboBox (amountOfTicket); adult.setEditable(false); adult.setSelectedItem (amountOfTicket[0]); adult.addActionListener (new EventHandler ()); }Last edited by mDennis10; 09-15-2011 at 03:23 AM.
-
Re: JComboBox Info
You'll need to tell us more. What is the original type of the item? Integer? If so then cast to this. If not, again, please don't hide the details of your problem if you need our help.
- 09-15-2011, 03:22 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 76
- Rep Power
- 0
Re: JComboBox Infoadult = new JComboBox (amountOfTicket);
Ok, i'm using a JComboBox component that I pass in a array of type string. which looks like this
What i want is that when the user selects on item a event is generated that gets the value the user selected so i can use it to do some calculations. The problem i am having is that i use the .getSelectedItem () method that returns on Object type which i don't know how to covert to an integer. So can u tell me how to convert it or how to get the data a different wayJava Code:private String [] amountOfTicket = {"0","1","2","3","4","5","6","7","8","9","10"}; private JComboBox adult; public void someFunction () { adult = new JComboBox (amountOfTicket); adult.setEditable(false); adult.setSelectedItem (amountOfTicket[0]); adult.addActionListener (new EventHandler ()); }
-
Re: JComboBox Info
getSelectedItem() will return one of the Objects in the combobox model -- but what did you fill it with? Strings! So what is returned is of course a String -- makes sense?
Two solutions that I know of:
1) Cast the Object to String and call Integer.parseInt(...) on it, or
2) Don't fill your combobox model with Strings but instead fill it with Integers (not ints), and then you can use the returned object (an Integer) directly.
Next time, please give us the information that you have in your second post in the original post as it will save both you and us much time and hassle. Please read my link below on how to ask smart questions for more tips that will help us help you.Last edited by Fubarable; 09-15-2011 at 03:27 AM.
- 09-15-2011, 03:35 AM #5
Member
- Join Date
- Jul 2011
- Posts
- 76
- Rep Power
- 0
-
Re: JComboBox Info
-
Re: JComboBox Info
Again, you may wish to show us your latest code attempt and the error message, else we can't tell what you did wrong.
- 09-15-2011, 04:13 AM #8
Member
- Join Date
- Jul 2011
- Posts
- 76
- Rep Power
- 0
Re: JComboBox Info
is whats happening nowJava Code:Integer amountOfTicket = {0,1,2,3,4,5};//new declaration int a =(int)adult.getSelectedItem();//get data from text field cannot cast from Object to intLast edited by mDennis10; 09-15-2011 at 04:19 AM.
- 09-15-2011, 04:20 AM #9
Member
- Join Date
- Jul 2011
- Posts
- 76
- Rep Power
- 0
Similar Threads
-
getting info from OS
By omriliba in forum Advanced JavaReplies: 8Last Post: 02-08-2011, 07:22 PM -
Getting info from a GUI
By waysgoose in forum New To JavaReplies: 1Last Post: 10-16-2010, 01:03 PM -
Activate JComboBox 1 when object is selected in JComboBox 2...
By bahumbaba in forum AWT / SwingReplies: 2Last Post: 12-10-2009, 01:58 PM -
Looking for some info.
By Ragster in forum JDBCReplies: 2Last Post: 01-17-2008, 05:19 PM -
JVM Info
By kishi in forum Advanced JavaReplies: 0Last Post: 11-16-2007, 01:44 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks