Details
Description
I use a DropDownChoice with a model of SelectOption objects and a ChoiceRenderer (see code below).
This is how I create a DropDownChoice instance:
DropDownChoice separatorDropDown = new DropDownChoice(
"separatorDropDown",
new PropertyModel(this, "separator"), //separator is a field in my class
new Model(new ArrayList(Arrays.asList(separators))), //separators is an array of SelectOption objects
choiceRenderer);
This is how the SelectOption looks like:
class SelectOption implements Serializable {
private String key;
private String value;
public SelectOption(String key, String value)
public String getKey() { return key; }
public void setKey(String key) { this.key = key; }
public String getValue() { return value; }
public void setValue(String value) { this.value = value; }
@Override
public String toString() { return getKey(); }
}
And this is how choiceRenderer looks like
ChoiceRenderer choiceRenderer = new ChoiceRenderer("key", "value");
These are the values that my model consist of:
private SelectOption[] separators = new SelectOption[]{ new SelectOption("comma", ","), new SelectOption("semicolon ", ";"), new SelectOption("dot", "."), new SelectOption("tab", "\t"), //causes null new SelectOption("tabandtex", "\ttext"), //causes null new SelectOption("space", " ")};
Whenever any item containing a \t (tab character) in its value field is selected, the separator field that PropertyModel points to becomes null. Otherwise correct SelectOption instance is being set into separator.
I noticed that tab characters are missing from the output markup generated by wicket.
<select wicket:id="separatorDropDown" ...>
<option selected="selected" value=",">comma</option>
<option value=";">semicolon </option>
<option value=".">dot</option>
<option value=" ">tab</option>
<option value=" text">tabandtex</option>
<option value=" ">space</option>
</select>
This is complete code:
Markup part:
<?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">
<html xmlns:wicket>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>BuggyPage</title>
</head>
<body>
<div>
Separator <select wicket:id="separatorDropDown">Separator</select>
</div>
<div wicket:id="separatorLabel">separatorLabel</div>
</body>
</html>
and Java code:
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
public final class BuggyPage extends WebPage {
//default separator
private SelectOption defaultSeparator = new SelectOption("comma", ",");
//current separator
private SelectOption separator = defaultSeparator;
//separators
private SelectOption[] separators = new SelectOption[]{ new SelectOption("comma", ","), new SelectOption("semicolon ", ";"), new SelectOption("dot", "."), new SelectOption("tab", "t"), //causes null new SelectOption("tabandtex", "ttext"), //causes null new SelectOption("space", " ")};
Label separatorLabel = new Label("separatorLabel", "Separator is " + separator);
public BuggyPage(PageParameters params) {
separatorLabel.setOutputMarkupId(true);
ChoiceRenderer choiceRenderer = new ChoiceRenderer("key", "value");
DropDownChoice separatorDropDown = new DropDownChoice(
"separatorDropDown",
new PropertyModel(this, "separator"),
new Model(new ArrayList(Arrays.asList(separators))),
choiceRenderer);
// Enable Ajax-based choice
separatorDropDown.add(new AjaxFormComponentUpdatingBehavior("onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) { //whenever the \t or \tsomething is selected, //the separator instance is null here. //This applies to any value that contains a backslash String separatorLabelText = "New separator is " + separator; Label newLabel = new Label("separatorLabel", separatorLabelText); newLabel.setOutputMarkupId(true); separatorLabel.replaceWith(newLabel); separatorLabel = newLabel; target.addComponent(newLabel); }
});
add(separatorDropDown);
add(separatorLabel);
}
class SelectOption implements Serializable {
private String key;
private String value;
public SelectOption(String key, String value) { this.key = key; this.value = value; }
/**
- @return the key
*/
public String getKey() { return key; }/**
- @param key the key to set
*/
public void setKey(String key) { this.key = key; }/**
- @return the value
*/
public String getValue() { return value; }/**
- @param value the value to set
*/
public void setValue(String value) { this.value = value; }@Override
{ return getKey(); }
public String toString()}
}