Wicket ListChoice example

In Wicket, you can use ListChoice to create a single select scrollable listbox.


//Java 
import org.apache.wicket.markup.html.form.ListChoice;
...
//choices in list box
private static final List<String> FRUITS = Arrays.asList(new String[] {
		"Apple", "Orang", "Banana" });

//variable to hold the selected list box value
private String selectedFruit = "Banana";

ListChoice<String> listFruits = new ListChoice<String>("fruit",
		new PropertyModel<String>(this, "selectedFruit"), FRUITS);
				
//HTML for single select listbox
<select wicket:id="fruit"></select>

1. Wicket single select listbox example

Example to display a single select scrollable listbox via “ListChoice“, and default a selected value.


package com.mkyong.user;

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.ListChoice;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.PropertyModel;

public class ListChoicePage extends WebPage {

	// single list choice
	private static final List<String> FRUITS = Arrays.asList(new String[] {
			"Apple", "Orang", "Banana" });

	// Banana is selected by default
	private String selectedFruit = "Banana";

	public ListChoicePage(final PageParameters parameters) {

		add(new FeedbackPanel("feedback"));

		ListChoice<String> listFruits = new ListChoice<String>("fruit",
				new PropertyModel<String>(this, "selectedFruit"), FRUITS);

		listFruits.setMaxRows(5);

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

				info("Selected Fruit : " + selectedFruit);

			}
		};

		add(form);
		form.add(listFruits);

	}
}

2. Wicket HTML page

Page to render the single select scrollable list.


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

	<div wicket:id="feedback"></div>
	<form wicket:id="form">
		<p>
			<label>[ListChoice] Select "ONE" of your favor fruit :</label> 
			<br />
			<select wicket:id="fruit"></select>
		</p>
		<input type="submit" value="Display" />
	</form>

</body>
</html>

3. Demo

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

“Banana” is selected automatically.

wicket listbox

Select “Banana” and click on the display button.

wicket listbox
Download it – Wicket-ListChoice-Examples.zip (7KB)

References

  1. Wicket ListChoice 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
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
checkburitoWicket Tutorial Recent comment authors
newest oldest most voted
burito
Guest
burito

BIG thanks for this article!

check
Guest
check

Please make a donation now!!!!

trackback
Wicket Tutorial

[…] multiple checkboxes. Radio buttons example Uses RadioChoice to render a group of radio buttons. Single select listbox example Uses ListChoice to render a single select scrollable list. Multi select listbox example Uses […]