在Wicket中,您可以使用ListChoice
创建一个单选滚动列表框 。
//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.小票单选列表框示例
通过“ ListChoice ”显示单个选择可滚动列表框,并默认为选定值的示例 。
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页面
页面以呈现单个选择的可滚动列表。
<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.演示
开始并访问-http:// localhost:8080 / WicketExamples /
自动选择“香蕉”。
![wicket-listchoice-example1 wicket listbox](https://www.mkyong.com/wp-content/uploads/2011/05/wicket-listchoice-example1.png)
选择“香蕉”,然后单击显示按钮。
![wicket-listchoice-example2 wicket listbox](https://www.mkyong.com/wp-content/uploads/2011/05/wicket-listchoice-example2.png)
下载它– Wicket-ListChoice-Examples.zip (7KB)