Wicket dropdown box example – DropDownChoice

In Wicket, you can use “DropDownChoice” to render a dropdown box component.


//Java 
import org.apache.wicket.markup.html.form.DropDownChoice;
...
//choices in dropdown box
private static final List<String> SEARCH_ENGINES = Arrays.asList(new String[] {
		"Google", "Bing", "Baidu" });

//variable to hold the selected value from dropdown box,
//and also make "Google" is selected by default
private String selected = "Google";

DropDownChoice<String> listSites = new DropDownChoice<String>(
		"sites", new PropertyModel<String>(this, "selected"), SEARCH_ENGINES);
				
//HTML for dropdown box
<select wicket:id="sites"></select>

1. Wicket DropDownChoice example

Example to display dropdown box via “DropDownChoice“, and default a selected value.


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

public class DropDownChoicePage extends WebPage {

	private static final List<String> SEARCH_ENGINES = Arrays.asList(new String[] {
			"Google", "Bing", "Baidu" });

	//make Google selected by default
	private String selected = "Google";
	
	public DropDownChoicePage(final PageParameters parameters) {

		add(new FeedbackPanel("feedback"));

		DropDownChoice<String> listSites = new DropDownChoice<String>(
			"sites", new PropertyModel<String>(this, "selected"), SEARCH_ENGINES);

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

				info("Selected search engine : " + selected);

			}
		};

		add(form);
		form.add(listSites);

	}
}

2. Wicket HTML page

Page to render a dropdown box.


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

	<div wicket:id="feedback"></div>
	<form wicket:id="form">
		<p>
			<label>Select your search engine </label> 
			<br /> 
			<select wicket:id="sites"></select>
		</p>
		<input type="submit" value="Display" />
	</form>

</body>
</html>

3. Demo

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

“Google” is selected by default.

wicket dropdown box

Select “Baidu” and click on the display button.

wicket dropdownbox example
Download it – Wicket-DropDownChoice-Example.zip (7KB)

References

  1. Wicket DropDownChoice 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
7 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
6 Comment authors
shrFarooq RahuFolarin????????Rafael Recent comment authors
newest oldest most voted
Folarin
Guest
Folarin

Hi, great article. I am a huge fan. Just a correction. I believe the comment //Google selected by default maybe wrong because the “selected” variable is only used to store result and the results are rendered according to the list order. Is there a way to do this without sorting the list?

shr
Guest
shr

Is it possible to do one tutorial on wicket and Google Map.

Farooq Rahu
Guest
Farooq Rahu

Thanks mkyong

????????
Guest
????????

Hello! I have a questions, can we generate in markup tags instate of tag? And if we can’t do that, how we can replace it with ListView? The problem is I have Page Navigation like drop down list with Ajax updating, but designer created markup with tags. Do you have any suggestions?

Rafael
Guest
Rafael

Congratulations, you wrote nice articles.

Frans
Guest
Frans

Oops that was mistake, example works fine!

Frans
Guest
Frans

Hello,

I copied and pasted the DropDownChoicePage example but I get the same error as in my real code: what ever I choose in the DropDownChoice I always get the first option instead of he choice. I am using wicket 1.4.19 Any clue?

Thanks