?
Solved

Swing combobox question

Posted on 2003-03-18
4
Medium Priority
?
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month12 days, 7 hours left to enroll

777 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