I need to get the integer value of the combobox in Swing.
I have set an integer value as id for the combobox.I tried combobox.getSelectedItem() and combobox.getSelectedIndex() but it cant get the int value.
Below is my code:
CommonBean commonBean[]=new CommonBean[commonResponse.getCommonBean().length+1];
for(int i=0;i<commonResponse.getCommonBean().length;i++)
{
commonBean[i] = new CommonBean("--Please select a project--", 0);
commonBean[i+1] = new CommonBean(commonResponse.getCommonBean()[i].getProjectName(), commonResponse.getCommonBean()[i].getProjectId());
}
JComboBox combobox= new JComboBox(commonBean);
public CommonBean(String projectName,int projectId) {
this.projectName = projectName;
this.projectId = projectId;
}
Any help is appreciated.
If the string is empty, comboBox.getSelectedItem().toString()
will give a NullPointerException
. So better to typecast by (String)
.
Method Object JComboBox.getSelectedItem()
returns a value that is wrapped by Object
type so you have to cast it accordingly.
Syntax:
YourType varName = (YourType)comboBox.getSelectedItem();`
String value = comboBox.getSelectedItem().toString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With