Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By mDennis10

Thread: JComboBox Info

  1. #1
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default 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
    Java 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 ());
    }
    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 way
    Last edited by mDennis10; 09-15-2011 at 03:23 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default 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
    Java 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 ());
    }
    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 way

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Re: JComboBox Info

    Quote Originally Posted by Fubarable View Post
    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.
    I Tried your second example but the compile is telling me can't covert from on object to int.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JComboBox Info

    Quote Originally Posted by mDennis10 View Post
    I Tried your second example but the compile is telling me can't covert from on object to int.
    Again Integer, not int, and again, you're telling us bits of your problem but not telling us the meat -- such as showing us the code and the actual error message.

    Please, read the link on how to ask smart questions.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  8. #8
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Re: JComboBox Info

    Java 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 int
    is whats happening now
    Last edited by mDennis10; 09-15-2011 at 04:19 AM.

  9. #9
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Re: JComboBox Info

    OOOOOOOOOO is see the problem now. i just saw the problem it is what u said, it should have been (Integer) and not (int). thanks a million
    DarrylBurke likes this.

Similar Threads

  1. getting info from OS
    By omriliba in forum Advanced Java
    Replies: 8
    Last Post: 02-08-2011, 07:22 PM
  2. Getting info from a GUI
    By waysgoose in forum New To Java
    Replies: 1
    Last Post: 10-16-2010, 01:03 PM
  3. Replies: 2
    Last Post: 12-10-2009, 01:58 PM
  4. Looking for some info.
    By Ragster in forum JDBC
    Replies: 2
    Last Post: 01-17-2008, 05:19 PM
  5. JVM Info
    By kishi in forum Advanced Java
    Replies: 0
    Last Post: 11-16-2007, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •