jsf: transfer data from one listbox to another

Is it possible in JSF to transfer data from one listbox to another with a button click? I can do this using plain javascript, but with JSF event handling possibilities can this be done? A sample code will helpthanks,balak
[242 byte] By [balaka] at [2007-10-3 1:16:48]
# 1

Easiesy way should be using the binding attribute.

JSF<h:selectOneListbox binding="#{myBean.listbox1}" />

<h:selectOneListbox binding="#{myBean.listbox2}" />

<h:commandButton action="#{myBean.moveData}" />

Beanprivate HtmlSelectOneListbox listbox1;

private HtmlSelectOneListbox listbox2;

// plus getters and setters

public void moveData() {

listbox2.setValue(listbox1.getValue());

}

Untested, but this might give you an idea anyway.

BalusCa at 2007-7-14 18:14:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
But, where will I sort? I want both the listboxes to be in sorted order whichever way I move data...........
balaka at 2007-7-14 18:14:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Use Collections.sort() in the getter of the data.
BalusCa at 2007-7-14 18:14:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Alternatively you could look at the source code for Apache Tomahawks PickList component. That is still in the sandbox at the moment but looking at the source code will get you there fairly well. http://myfaces.apache.org/sandbox/selectManyPicklist.html
paul_norriea at 2007-7-14 18:14:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

[code]Hi,

this is one way it is right or wrong i dont know but it is working for me

jsp page

<h:panelGroup>

<h:selectOneMenu id="host" binding="#{HRselforeachappvm.selectonelist}" value="#{HRselforeachappvm.host}"

valueChangeListener="#{HRselforeachappvm.hostSelection}" onchange="submit();"immediate="true" required="true">

<f:selectItem itemValue="" itemLabel=""/>

<f:selectItems value="#{HRselforeachappvm.hosts}"/>

</h:selectOneMenu>

<h:message for="host" style="color:red"/>

</h:panelGroup>

<h:outputText value="SelectAPPlication" />

<h:panelGroup>

<h:selectOneMenu id="appcpu" value="#{HRselforeachappvm.user}" required="true">

<f:selectItem itemValue="" itemLabel=" "/>

<f:selectItems value="#{HRselforeachappvm.users}"/>

</h:selectOneMenu>

backing bean

public void setSelectonelist(UISelectOne selectone){

this.selectone=selectone;

}

public UISelectOne getSelectonelist(){

return selectone;

}

public void hostSelection(ValueChangeEvent ee)

throws AbortProcessingException

{

System.out.println("valuechangeevent");

FacesContext context = FacesContext.getCurrentInstance();

getUsers();

context.renderResponse();

}

public Map getUsers(){

try{

if(selectone!=null)

hostselected= (String)selectone.getLocalValue();// v .v imp note

else

hostselected=" ";

System.out.println("getting the selectedhost"+hostselected);

openConnection();

String str="select distinct pd_user from sp_process_detail_current where host_id=?";

System.out.println("setting users from host");

ps=con.prepareStatement(str);

ps.setString(1,hostselected);

ps.execute();

rs=ps.getResultSet();

while(rs.next()){

pd_user=rs.getString("pd_user");

System.out.println(pd_user);

selusernames.put(pd_user,pd_user);

}

return selusernames;

}

catch(SQLException e){

e.printStackTrace();

}

catch(Exception e){

e.printStackTrace();

}

finally{

try{

if(rs!=null)

rs.close();

rs=null;

}

catch(SQLException e){

e.printStackTrace();

}

try{

if(ps!=null)

ps.close();

ps=null;

}

catch(SQLException e){

e.printStackTrace();

}

try{

close();

}

catch(SQLException e){

e.printStackTrace();

}

}

return selusernames;

}

[/code]

shannua at 2007-7-14 18:14:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Looks fine. Onlyonchange="submit();"seems useless as you already have implemented valueChangeListener.
BalusCa at 2007-7-14 18:14:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

[b]HI BalusC ,

ya but without local submitting onchange="submit();"

value change listener does not work.

actually the value change listener example

is like that in core java server faces.

but i tried with out using local submitting

onchange=submi();"

but it is not working

you also try that

with regards

shannu sarma[/b]

Message was edited by:

shannu

Message was edited by:

shannu

shannua at 2007-7-14 18:14:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...