Wicket multiple checkboxes example – CheckBoxMultipleChoice

Wicket example to create multiples select checkboxes, and checked the checkbox automatically.


//Java 
import org.apache.wicket.markup.html.form.CheckBoxMultipleChoice;
...
//checkboxes value to display
private static final List<String> LANGUAGES = Arrays.asList(new String[] {
			"Java", ".NET", "PHP", "Ruby", "C/C++" });
			
//variable to hold the checkbox values
private ArrayList<String> languagesSelect = new ArrayList<String>();

final CheckBoxMultipleChoice<String> listLanguages = 
               new CheckBoxMultipleChoice<String>(
			"languages", new Model(languagesSelect), LANGUAGES);
				
//HTML
<span wicket:id="languages"></span>

1. Wicket multiple checkboxs example

Example to display multiple checkboxes via “CheckBoxMultipleChoice“, and checked it automatically. The code should be self-explanatory.

File : CheckBoxMultipleChoicePage.java


package com.mkyong.user;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.CheckBoxMultipleChoice;
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;

public class CheckBoxMultipleChoicePage extends WebPage {

	private static final List<String> LANGUAGES = Arrays.asList(new String[] {
			"Java", ".NET", "PHP", "Ruby", "C/C++" });

	private static final List<String> HOSTING_TYPES = Arrays
			.asList(new String[] { "Shared Host", "VPS", "Clound Host",
					"Dedicated Server" });

	// hold the checkbox values
	private ArrayList<String> languagesSelect = new ArrayList<String>();

	// checked vps and dedicated server by default
	private ArrayList<String> hostingtSelect = new ArrayList<String>(
			Arrays.asList(new String[] { "VPS", "Dedicated Server" }));

	public CheckBoxMultipleChoicePage(final PageParameters parameters) {

		add(new FeedbackPanel("feedback"));

		final CheckBoxMultipleChoice<String> listLanguages = 
                           new CheckBoxMultipleChoice<String>(
				"languages", new Model(languagesSelect), LANGUAGES);

		final CheckBoxMultipleChoice<String> listHosting = 
                           new CheckBoxMultipleChoice<String>(
				"hostings", new Model(hostingtSelect), HOSTING_TYPES);

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

				info("Languages : " + languagesSelect.toString());
				info("Hosting Types : " + hostingtSelect.toString());

			}
		};

		add(form);
		form.add(listLanguages);
		form.add(listHosting);

	}
}

2. Wicket HTML page

Page to render multiple checkboxes.

File : CheckBoxMultipleChoicePage.html


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

	<div wicket:id="feedback"></div>
	<form wicket:id="userForm">
		<p>
			<label>1. Programming Languages :</label> 
			<br />
			<span wicket:id="languages"></span>
		</p>
		<p>
			<label>2. Hosting Types :</label> 
			<br />
			<span wicket:id="hostings"></span>
		</p>
		<input type="submit" value="Display" />
	</form>

</body>
</html>

3. Demo

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

By default, “VPS” and “dedicated server” are checked.

wicket multiple checkboxes

Select some checkboxes and click on the display button.

wicket multiple checkboxs

References

  1. Wicket CheckBoxMultipleChoice Javadoc

About the Author

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.

Comments

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Suresh Akella Recent comment authors
newest oldest most voted
Suresh Akella
Guest
Suresh Akella

Hi,
I am trying to get the selected values on AjaxLink submit but it is not working. I need Ajax Behaviour because I have to redisplay the selected values(of CheckBox Group) in a list view beneath it. So, after the selection is made, I added a “AjaxLink” to do ajax submit and trying to get those values. But I am getting only the last selected check box item. Any help is highly appreciated.