How can I set the selected NativeSelected item?

Hello!

I have a Native Select. The data source is a result of a Table Query. It include the id and name. The caption of items is the name. I have an id in a variable (I have got it from another query)… How can I set the selected item with this id?

Thanks!

Hi,

I’ve had the same problem but I didn’t find solution. I did like that: manually get Items from db and put all of these in loop like that:

int i = 0;
for (String s: dbData) {
  nsSelect.addItem(i);
  nsSelect.setItemCaption(i++, s);
}

and we can set selected item by method nsSelect.select(myItem);

Best regards,
Paul

Thank you Paul, although it’s quite bad news: (

Hi,

Probably I do understand why it doesn’t work. Because Container assigned to NativeSelect at first time doesn’t have items. And client side doesn’t know what’s item contain this compoment and it cannot find data. It should be done like that, after get your Id, this component must retrieve data from DB and update view.

If I’m wrong, correct me…

Best regards,
Paul

Thank you Paul, I can solve it with your sample.


ResultSet rs = stmt.executeQuery(sql);
	        while ( rs.next()) {
	        		nselect.addItem( rs.getInt("id") );
	        		nselect.setItemCaption( rs.getInt("id"), rs.getString("field") );
	        }