
I decided that I should publish it along with the references to the other articles.
JSF has a number of component frameworks available from groups like PrimeFaces and RichFaces. These are better choices than this code to accomplish the same thing.
However, you don't need all the additional code to do the same thing with plain old vanilla JSF. You will just need to write all the code.
The code for this project can be found here: switchlist-example
index.xhtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> > < h:head > < meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" /> < title >Switchlist Example</ title > < h:outputStylesheet library = "css" name = "switchlist.css" /> </ h:head > < h:body > < h:form id = "form1" > < h:selectManyListbox id = "list1" value = "#{listHolder.list1}" styleClass = "switchlist" > < f:selectItems value = "#{listHolder.items1}" /> </ h:selectManyListbox > < h:panelGroup id = "buttonGroup1" styleClass = "switchlistButtons" > < h:commandButton id = "button1" value = "→" actionListener = "#{listHolder.move1to2}" styleClass = "switchlistButton" > < f:ajax execute = "form1:button1 form1:list1" render = "form1:list1 form1:list2" /> </ h:commandButton > < h:commandButton id = "button2" value = "←" actionListener = "#{listHolder.move2to1}" styleClass = "switchlistButton" > < f:ajax execute = "form1:button2 form1:list2" render = "form1:list1 form1:list2" /> </ h:commandButton > </ h:panelGroup > < h:selectManyListbox id = "list2" value = "#{listHolder.list2}" styleClass = "switchlist" > < f:selectItems value = "#{listHolder.items2}" /> </ h:selectManyListbox > < h:commandButton value = "Reset" type = "submit" actionListener = "#{listHolder.resetLists()}" > < f:ajax execute = "@form" render = "form1" /> </ h:commandButton > < h:messages /> </ h:form > </ h:body > </ html > |
ListHolder.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | package com.bluelotussoftware.jsf; import java.io.Serializable; import java.util.LinkedHashMap; import java.util.Map; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; import javax.faces.event.ActionEvent; /** * * @author Jim Driscoll * @author John Yeary * @version 2.0 */ @ManagedBean @ViewScoped public class ListHolder implements Serializable { private static final long serialVersionUID = -933704272568697599L; private final Map<String, String> items1 = new LinkedHashMap<String, String>(); private final Map<String, String> items2 = new LinkedHashMap<String, String>(); private String[] list1 = null ; private String[] list2 = null ; public ListHolder() { resetLists(); } public void move1to2(ActionEvent event) { if (list1 != null && list1.length > 0 ) { for (String item : list1) { items2.put(item, items1.remove(item)); } } } public void move2to1(ActionEvent event) { if (list2 != null && list2.length > 0 ) { for (String item : list2) { items1.put(item, items2.remove(item)); } } } public Map<String, String> getItems1() { return items1; } public Map<String, String> getItems2() { return items2; } public String[] getList1() { return list1; } public void setList1(String[] list1) { this .list1 = list1; } public String[] getList2() { return list2; } public void setList2(String[] list2) { this .list2 = list2; } public void resetLists() { items1.clear(); items2.clear(); items1.put( "one" , "one" ); items1.put( "two" , "two" ); items1.put( "three" , "three" ); items1.put( "four" , "four" ); items2.put( "five" , "five" ); items2.put( "six" , "six" ); items2.put( "seven" , "seven" ); items2.put( "eight" , "eight" ); } } |
0 comments :
Post a Comment