Wicket ListMultipleChoice example

In Wicket, you can use ListMultipleChoice to create a multi select scrollable listbox.


//Java 
import org.apache.wicket.markup.html.form.ListMultipleChoice;
...
//choices in list box
private static final List<String> NUMBERS = Arrays.asList(new String[] {
			"Number 1", "Number 2", "Number 3", "Number 4", "Number 5",
			"Number 6" });

//variable to hold the selected multiple values from listbox, 
//and make "Number 6" selected as default value
	private ArrayList<String> selectedNumber = new ArrayList<String>(
		Arrays.asList(new String[] { "Number 6" }));

ListMultipleChoice<String> listNumbers = new ListMultipleChoice<String>(
		"number", new Model(selectedNumber), NUMBERS);
				
//HTML for multiple select listbox
<select wicket:id="number"></select>

1. Wicket multiple select listbox example

Example to display a multiple select scrollable listbox via “ListMultipleChoice“, and default a selected value.


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.Form;
import org.apache.wicket.markup.html.form.ListMultipleChoice;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.Model;

public class ListMultipleChoicePage extends WebPage {

	private static final List<String> NUMBERS = Arrays.asList(new String[] {
			"Number 1", "Number 2", "Number 3", "Number 4", "Number 5",
			"Number 6" });

	// Number 6 is selected by default
	private ArrayList<String> selectedNumber = new ArrayList<String>(
			Arrays.asList(new String[] { "Number 6" }));

	public ListMultipleChoicePage(final PageParameters parameters) {

		add(new FeedbackPanel("feedback"));

		ListMultipleChoice<String> listNumbers = new ListMultipleChoice<String>(
				"number", new Model(selectedNumber), NUMBERS);

		listNumbers.setMaxRows(5);

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

				info("Selected Number : " + selectedNumber);

			}
		};

		add(form);
		form.add(listNumbers);

	}
}

2. Wicket HTML page

Page to render the multiple select scrollable list.


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

	<div wicket:id="feedback"></div>
	<form wicket:id="form">
		<p>
			<label>[ListMultipleChoice] select "MULTIPLE" of your favor
				number : (Ctrl + left click)</label> 
			<br /> 
			<select wicket:id="number"></select>
		</p>
		<input type="submit" value="Display" />
	</form>

</body>
</html>

3. Demo

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

“Number 6” is selected automatically.

wicket listbox

Select “Number 2, 4, 6” and click on the display button.

wicket listbox

References

  1. Wicket ListMultipleChoice 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
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
josiane Oliveirawill824 Recent comment authors
newest oldest most voted
josiane Oliveira
Guest
josiane Oliveira

Hello, I would like to know, how can I put the list from an table of database?

will824
Guest
will824

Hi MKyong. Thanks for the post.
I have a question. I am trying to add an Ajax Behavior to the ListMultipleChoice component without succes. I am getting an exception if I try to add an AjaxFormChoiceComponentUpdatingBehavior saying that can only be added to an instance of a RadioChoice/CheckboxChoice/RadioGroup/CheckGroup. Do you know if it is compatible with other Ajax Behavior?