cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Programatically Select ListBox Item in Java Code

ptc-1894556
1-Newbie

Programatically Select ListBox Item in Java Code

Hi - I found Keith Berards sample (below) for selecting a listbox item in Java but am having trouble getting it to work. What we want is that as the user sets focus to various nodes in the document edit window that a specific item in a listbox residing in an XUI dialog is selected programattically. All the event handling is working fine to do this and debug traces show that the listBox.setAttribute("value", whatever); is being executed as the user focus on document nodes.

However after this code is executed the required line in the listbox is still not hilighted. Could this be that the XUI dialog does not have focus (the edit window does). Any other ideas what might be wrong here most welcome. This seems like it should be pretty simple..


David

<listbox id="myListBox">
<listitem label="One" appdata="1"/>
<listitem label="Two" appdata="2"/>
<listitem label="Three" appdata="3"/>
</listbox>

I have found that while there is no way to select by index or value, you can select an item in a listbox by setting the "value" attribute in the listbox to the value of the "label" attribute of the item within the list.

Here's a snippet of code (java) that selects an item in a list based on its id (set in appdata).

public boolean selectListItem(String id) {

NodeList children = this.listBox.getChildNodes();

String curText = ";

boolean found = false;

for (int i=0; i<children.getlength(); i++)=" {<br="/>
Element curChild = (Element)children.item(i);

if (curChild.getAttribute("appdata").equals(id)) {
curText = curChild.getAttribute("label");
this.listBox.setAttribute("value",curText);
found = true;
break;
}
}

return found;
}
2 REPLIES 2

Hmm, we're still using this code in production and it works fine... let me
have a look again.

Here's an actual code snippet from a class we use to wrap our listboxes.

/**
*
*/
public boolean selectListItem(String id) {

NodeList children = this.listBox.getChildNodes();

String curText = ";

boolean found = false;

for (int i=0; i<children.getlength(); i++)=" {<br="/>
Element curChild = (Element)children.item(i);

if (curChild.getAttribute("appdata").equals(id)) {
curText = curChild.getAttribute("label");
this.listBox.setAttribute("value",curText);
found = true;
break;
}
}

this.setDisplayText(curText);

return found;

}


Did you try yours with a very simple list first? (say, making a button
select, rather than rely on the hover at first)

keith

Sorry, I just now realized that I pasted pretty much the exact same thing
you originally quoted...

Let me know if there's something more specific I can provide.

keith

Announcements