How to programmatically select an item in a listbox

Keith Berard shows the trick Last Updated: 2006-09-12

I'm going to assume for now you have a handle on your XUI document and that you're writing all of this in java. If this is not the case, it should be fairly simple to write this in rhino-javascript as well. Let me know.

According to the customizer's guide (just search for "getElementById") Arbortext suggests that you set the "id" attribute on your listbox.

e.g.: 

From there, you can select the box by calling:

Element myListBox = myXuiDoc.getElementById("myListBox");

From there, you should be able to call the function below.

As for appdata... the documentation also specifies "appdata" as a pre-defined attribute to be used for custom values on the XUI nodes. So, you could set your listbox to something like:

     

I have found that you can also set pretty much any other attribute value you want for this (since it is just a dom node after all) but I avoid this if I can because that attribute _may_ become used by Arbortext in a later XUI version (assuming XUI is still being developed)

This works well for a XUI file that you have predefined (myXui.xml), but I believe it has some drawbacks, especially with large XUI documents. We have implemented all of our production XUI to be created on the fly in java code, and I'll post another followup on this. I mentioned this back in December, but have been so busy I didn't get a chance to put something together. Since I have some time today, I'll try to put a complete example together since there was some interest from others back then as well.

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.

This of course only works if all listitems are distinct, and I have had to resort to numbering my listitems to keep them that way.

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++) { 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; }