Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ArrayList and ComboBox 1

Status
Not open for further replies.

k4ghg

Technical User
Dec 25, 2001
191
US
I am trying to store a combo box's item index into an arraylist and am getting a compiler error. I tried several different approaches and none worked.

1 -
aSoilIndex.add(newCounter, cboSoil.getSelectedIndex());

2 -
aSoilIndex.add(newCounter, (int)
cboSoil.getSelectedIndex());

3 -
int SoilIndextemp = (int) cboSoil.getSelectedIndex();
aSoilIndex.add(newCounter, SoilIndextemp);

Any suggestions will be appreciated. Thanks Ronnie
 
And your compiler error is ?
And what are all the object types (or should be guess !) ?

The ArrayList.add() method has two signatures :

add(int index, Object element)

add(Object element)

I don't know what the type "newCounter" is, but I can certainly see that your second parameter is an int - which is not an Object ...

Click here to learn Ways to help with Tsunami Relief
--------------------------------------------------
Free Database Connection Pooling Software
 
I agree sedj, you cannot add a primitive type to an ArrayList. You need to encapsulate it in an Object, ie, an Integer.

Cheers,

Dian
 
Sorry for leaving out parts of the code. I was trying
the ArrayList.add() method with the add(int index, Object element)signiture.

Origionally I tried the following code:

aSoilIndex.add(newCounter, cboSoil.getSelectedIndex());

where: newCounter was delcared an integer, cboSoil was declared a JCombobox and aSoilIndex an Arraylist.

I don't remember the exact error because the code is on another computer. In general it seemed to relate to cboSoil.getSelectedIndex(). It said that it was not an object and even after I tried to case it to an object it gave me an error. Thats when I tried to cast it to an integer. Thanks again
 
Well it sounds like getSelectedIndex() returns an int - which is a primitive type, not an object.

You should do :

aSoilIndex.add(newCounter, new Integer(cboSoil.getSelectedIndex()));



Click here to learn Ways to help with Tsunami Relief
--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top