using two list boxes ?

hello

I want to build something where the user has two listboxes and should be able

to move items from one to the other by clicking on a button between the listboxes.

I thought about using two separate boxes and then Java Script to copy the elements

from one box to the other.

I am new to JSF.

is there a better way to do this ? maybe a standard component that does this.

I couldn't find it in my book (Java Server Faces The Complete Reference).

best wishes

Thomas

[526 byte] By [ThomBreita] at [2007-11-26 15:15:58]
# 1

Hi Thomas,

JSF does support one component called "AddRemoveList" that suits your requirements.

You can use this component straight away without having to write too much of Javascript code.

For the detailed help on it, I would recommend you to visit the following link:

http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/about_components.html

JSFBuga at 2007-7-8 10:47:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

This is simple:

JSF<h:form>

<h:selectManyMenu value="#{myBean.selectedItemsLeft}">

<f:selectItems value="#{myBean.selectItemsLeft}" />

</h:selectManyMenu>

<h:commandButton value="left to right" action="#{myBean.leftToRight}" />

<h:commandButton value="right to left" action="#{myBean.rightToLeft}" />

<h:selectManyMenu value="#{myBean.selectedItemsRight}">

<f:selectItems value="#{myBean.selectItemsRight}" />

</h:selectManyMenu>

</h:form>

MyBean (session scoped)public class MyBean {

private List selectItemsLeft = new ArrayList();

private List selectedItemsLeft;

private List selectItemsRight = new ArrayList();

private List selectedItemsRight;

// + getters + setters

public MyBean() {

loadSelectItemsLeft();

}

public void leftToRight() {

for (Iterator iter = selectItemsLeft.iterator(); iter.hasNext();) {

SelectItem selectItem = (SelectItem) iter.next();

if (selectedItemsLeft.contains(selectItem.getValue())) {

selectItemsRight.add(selectItem);

iter.remove();

}

}

selectedItemsLeft.clear();

}

public void rightToLeft() {

for (Iterator iter = selectItemsRight.iterator(); iter.hasNext();) {

SelectItem selectItem = (SelectItem) iter.next();

if (selectedItemsRight.contains(selectItem.getValue())) {

selectItemsLeft.add(selectItem);

iter.remove();

}

}

selectedItemsRight.clear();

}

private void loadSelectItemsLeft() {

selectItemsLeft.add(new SelectItem("key1", "value1"));

selectItemsLeft.add(new SelectItem("key2", "value2"));

selectItemsLeft.add(new SelectItem("key3", "value3"));

selectItemsLeft.add(new SelectItem("key4", "value4"));

selectItemsLeft.add(new SelectItem("key5", "value5"));

}

}

BalusCa at 2007-7-8 10:47:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...