Two Laptop Bag*
The moose likes JSF and the fly likes Need Help with Editable Combo/Dropdown  Box in JSF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Need Help with Editable Combo/Dropdown  Box in JSF" Watch "Need Help with Editable Combo/Dropdown  Box in JSF" New topic
Author

Need Help with Editable Combo/Dropdown Box in JSF

Gus Johnson
Greenhorn

Joined: Mar 14, 2006
Posts: 4
Hi everybody,

I am developing a web application using JSF, that needs a dropdown box which is pre-populated with common values. But if the user doesn't find the value they need, they should be able to enter one. Is there any such component in JSF? If not, are there alternatives to solving this issue?

Thanks for your help.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15964
    
  19

There's no standard combo box control in JSF. One can be constructed using a dropdown list + a textbox + some JavaScript.

It's possible someone already has made a JSF custom control like this so you don't have to do all the work yourself - I just don't know of any offhand.


Customer surveys are for companies who didn't pay proper attention to begin with.
Tak Ng
Greenhorn

Joined: Jun 05, 2007
Posts: 19
You might use a workaround. Have the drop down list activates another textfield only when the user selects "Other" option. Tell me if you need a working example on how to do it.
Gus Johnson
Greenhorn

Joined: Mar 14, 2006
Posts: 4
Thanks Tak Ng, I would appreciate a working example, if you have one available...
Tak Ng
Greenhorn

Joined: Jun 05, 2007
Posts: 19
These are the steps taken by JSF life cycle:
1) if the drop down menu changes, selectedOptionChanged will get called.
2) if the value of selectedOption is "other", the flag activateOtherInput is activated; otherwise, it is off.
3) the other input textfield appears depending on the flag activateOtherInput.

The disadvantage of this method is an additional round trip to the server, but you avoid writing your own javascript.

class ExampleForm{
private String selectedOption;
private String otherInput;
private boolean activateOtherInput;

public void selectedOptionChanged(ValueChangeEvent event){
if(selectedOption.equals("other") //Or whatever value you might have
activateOtherInput = true;
else
activateOtherInput = false;
FacesContext context = FacesContext.getCurrentInstance();
selectedOption = (String)event.getNewValue();
context.renderResponse();
}
...
//Rest of code
}

example.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

<html><head></head><body>
<h:selectOneMenu value="#{exampleForm.selectedOption}" onchange="submit();"
valueChangeListener="#{exampleForm.selectedOptionChanged}">
<f:selectItem value="PC" label="PC"/>
<f:selectItem value="Mac" label="Mac"/>
<f:selectItem value="other" label="Other"/>
</h:selectOneMenu>
<h:inputText value="#{exampleForm.otherInput}" rendered="#{exampleForm.activateOtherInput}"></f:inputText>
</body>
</html>
Gus Johnson
Greenhorn

Joined: Mar 14, 2006
Posts: 4
Thanks a million, Tak Ng...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need Help with Editable Combo/Dropdown Box in JSF
 
Similar Threads
JSON+Jquery
How to create a h:selectOneMenu with a default value?
combo box valildations
Doubt in h:selectItems
JSF drop down.