Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Swing combobox question

Posted on 2003-03-18
4
Medium Priority
?
230 Views
Last Modified: 2010-08-05
how can you get an int value from a combo box?

I tried this:

final JComboBox ansBox= new JComboBox();
     
     // populate the combo box
     for (int i=1; i<=10; i++) {
         ansBox.addItem(""+i);
     }
     ansBox.setSelectedIndex(0);

then when trying to get the value back out I did:

int number = ((Integer) ansBox.getSelectedItem()).intValue();
.....

but it complains, how do you do it properly?!

cheers              
0
Comment
Question by:gingermeatboy
  • 2
  • 2
4 Comments
 

Author Comment

by:gingermeatboy
ID: 8163269
I've changed it so for the above example I just put

int number = (ansBox.getSelectedIndex()+1);

but is there a better way?
0
 
LVL 3

Accepted Solution

by:
allahabad earned 200 total points
ID: 8163503
final JComboBox ansBox= new JComboBox();
   
    // populate the combo box
    for (int i=1; i<=10; i++) {
        ansBox.addItem(String.valueOf(i));
    }
    ansBox.setSelectedIndex(0);
int number = Integer.parseInt((String)ansBox.getSelectedItem());
0
 

Author Comment

by:gingermeatboy
ID: 8163543
thanks! :o)
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8163567
You are welcome.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question