Wicket checkbox example

Wicket example to create a checkbox, and checked the checkbox automatically.


//Java 
import org.apache.wicket.markup.html.form.CheckBox;
...
final CheckBox chk0 = new CheckBox("checkbox0", Model.of(Boolean.TRUE));	
final CheckBox chk1 = new CheckBox("checkbox1", new PropertyModel<Boolean>(this, "checkbox1"));
form.add(address);

//HTML
<input type="checkbox" wicket:id="checkbox0" />
<input type="checkbox" wicket:id="checkbox1" />

1. Wicket checkbox example

Different ways to render checkboxes, checked the “chk0” and “chk2” by default.

Checked by default ?
To checked a checkbox, you just need to assign a “true” to the component. This concept applied to other web application frameworks as well.

File : CheckBoxPage.java


package com.mkyong.user;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

public class CheckBoxPage extends WebPage {

	private boolean checkbox1 = false; // uncheck
	private boolean checkbox2 = true; // checked by default

	public CheckBoxPage(final PageParameters parameters) {

		add(new FeedbackPanel("feedback"));

		final CheckBox chk0 = new CheckBox("checkbox0", Model.of(Boolean.TRUE));

		final CheckBox chk1 = new CheckBox("checkbox1",
				new PropertyModel<Boolean>(this, "checkbox1"));

		final CheckBox chk2 = new CheckBox("checkbox2",
				new PropertyModel<Boolean>(this, "checkbox2"));

		Form<?> form = new Form<Void>("userForm") {
			@Override
			protected void onSubmit() {

				info("checkbox0 : " + chk0.getModelObject().toString());
				info("checkbox1 : " + Boolean.toString(checkbox1));
				info("checkbox2 : " + Boolean.toString(checkbox2));

			}
		};

		add(form);
		form.add(chk0);
		form.add(chk1);
		form.add(chk2);
	}
}

2. Wicket HTML page

Page to render 3 checkboxes.

File : CheckBoxPage.html


<html>
<head>
<style>
label {
	background-color: #eee;
	padding: 4px;
	float:left;
}

.feedbackPanelINFO {
	color: green;
}
</style>
</head>
<body>
	<h1>Wicket Checkbox Example</h1>

	<div wicket:id="feedback"></div>
	<form wicket:id="userForm">
		<p>
			<label>CheckBox 0 :</label> 
			<input type="checkbox" wicket:id="checkbox0" />
		</p>
		<p>
			<label>CheckBox 1 :</label> 
			<input type="checkbox" wicket:id="checkbox1" />
		</p>
		<p>
			<label>CheckBox 2 :</label> 
			<input type="checkbox" wicket:id="checkbox2" />
		</p>
		<input type="submit" value="Register" />
	</form>

</body>
</html>

3. Demo

Start and visit – http://localhost:8080/WicketExamples/

The “chk0” and “chk2” are checked by default.

wicket checkbox
wicket checkbox example
Download it – Wicket-Checkbox-Example.zip (7KB)

References

  1. Wicket Checkbox Javadoc
author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
3000
newest oldest most voted
Remonty Kraków
Guest
Remonty Kraków

Hi I love your work it’s always very helpful but this one can get little confisung when you take a first look. The first code box isint costistend with second one and there is this weird form.add(address); Maybe im just overreactiong but maybe you should take a look.

Marc Schneider
Guest
Marc Schneider

Your Blog is great! Good work 🙂

Alan Christian
Guest
Alan Christian

I really like this blog, is very useful and straightforward when doubts.

trackback
Wicket Tutorials

[…] Checkbox example Uses Checkbox to render checkbox, and checked it by default. […]