Skip to content

Instantly share code, notes, and snippets.

@mattbrictson
Last active June 17, 2018 23:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattbrictson/1323874 to your computer and use it in GitHub Desktop.
Save mattbrictson/1323874 to your computer and use it in GitHub Desktop.
How to Implement Radio Buttons in Wicket
<input type="radio" name="ticker" value="AAPL"/>Apple<br/>
<input type="radio" name="ticker" value="GOOG"/>Google<br/>
<input type="radio" name="ticker" value="MSFT"/>Microsoft<br/>
<label><input type="radio" name="ticker" value="AAPL"/>Apple</label>
<label><input type="radio" name="ticker" value="GOOG"/>Google</label>
<label><input type="radio" name="ticker" value="MSFT"/>Microsoft</label>
label { display: block; }
<form wicket:id="form">
<wicket:container wicket:id="group">
<label><input wicket:id="aapl" type="radio" name="ticker"/>Apple</label>
<label><input wicket:id="goog" type="radio" name="ticker"/>Google</label>
<label><input wicket:id="msft" type="radio" name="ticker"/>Microsoft</label>
</wicket:container>
</form>
// Radio buttons must be part of a Form component.
Form form = new Form("form");
add(form);
// This model holds the value corresponding to the radio that the user selected.
// We use a simple Model here, but in practice it would be a PropertyModel to your
// persistent data object.
IModel<String> selected = new Model<String>();
RadioGroup group = new RadioGroup("group", selected);
form.add(group);
// For each radio button, we create it with a hardcoded string value that
// represents that choice. That string value will be passed to our selected
// model (see above) when the form is submitted.
group.add(new Radio("aapl", new Model<String>("AAPL")));
group.add(new Radio("goog", new Model<String>("GOOG")));
group.add(new Radio("msft", new Model<String>("MSFT")));
public class Company implements Serializable
{
private String symbol;
private String name;
// getters and setters omitted for brevity
/**
* It is very important to provide a meaningful {@code equals()}
* implementation. Wicket uses this to compare the saved value of the
* form against the various radio button choices in order to determine
* which choice should be drawn as selected.
*/
public boolean equals(Object other)
{
if(!(other instanceof Company)) return false;
return symbol != null && symbol.equals(((Company) other).symbol);
}
}
<form wicket:id="form">
<wicket:container wicket:id="group">
<label wicket:id="choice">
<input wicket:id="radio" type="radio" name="ticker"/>
<wicket:container wicket:id="label"/>
</label>
</wicket:container>
</form>
// Radio buttons must be part of a Form component.
Form form = new Form("form");
add(form);
IModel<Company> selected = new Model<Company>();
RadioGroup group = new RadioGroup("group", selected);
form.add(group);
// Exercise left for the reader: load the list of companies from the database.
IModel<List<Company>> companies;
// Construct a radio button and label for each company.
group.add(new ListView<Company>("choice", companies) {
protected void populateItem(ListItem<Company> it)
{
it.add(new Radio("radio", it.getModel()));
it.add(new Label("label", it.getModelObject().getName()))
}
});
<input type="radio" name="form:group" value="radio13"/>
IModel<Company> selected = new Model<Company>();
// Exercise left for the reader: load the list of companies from the database.
IModel<List<Company>> companies;
// Tell RadioChoicesListView what properties to use for label and ID
ChoiceRenderer<Company> renderer = new ChoiceRenderer<Company>("name", "symbol");
// Wire it all up!
add(new Form("form")
.add(new RadioGroup("group", selected)
.add(new RadioChoicesListView("choice", companies, renderer))));
@Zhazhanan
Copy link

class 'RadioChoicesListView' in wicket which version,I didn't find it

@AlexisMontet
Copy link

It was very useful. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment