JavaFX UI控件教程(十二)之List View
翻譯自??List View
在本章中,您將學(xué)習(xí)如何在JavaFX應(yīng)用程序中創(chuàng)建列表。
該ListView級代表項(xiàng)目的滾動列表。圖11-1顯示了酒店預(yù)訂系統(tǒng)中可用住宿類型的列表。
圖11-1簡單列表視圖
您可以通過使用該setItems方法定義其項(xiàng)目來填充列表。您還可以通過應(yīng)用setCellFactory方法為列表中的項(xiàng)創(chuàng)建視圖。
?
創(chuàng)建列表視圖
例11-1中的代碼片段實(shí)現(xiàn)了包含圖11-1String中所示項(xiàng)的列表。
示例11-1創(chuàng)建列表視圖控件
ListView<String> list = new ListView<String>(); ObservableList<String> items =FXCollections.observableArrayList ("Single", "Double", "Suite", "Family App"); list.setItems(items);要更改列表視圖控件的大小和高度,請使用setPrefHeight和setPrefWidth方法。例11-2將垂直列表限制為100像素寬,70像素高,這導(dǎo)致列表如圖11-2所示。
示例11-2設(shè)置列表視圖的高度和寬度
list.setPrefWidth(100); list.setPrefHeight(70);
圖11-2調(diào)整大小的垂直列表
您可以ListView通過將orientation屬性設(shè)置為水平定向?qū)ο驩rientation.HORIZONTAL。這可以按如下方式完成:list.setOrientation(Orientation.HORIZONTAL)。與圖11-1中相同項(xiàng)目的水平列表如圖11-3所示。
圖11-3水平列表視圖控件
您可以隨時(shí)ListView使用SelectionModel和FocusModel類跟蹤對象的選擇和焦點(diǎn)。要獲取每個(gè)項(xiàng)目的當(dāng)前狀態(tài),請使用以下方法的組合:
-
getSelectionModel().getSelectedIndex()?- 以單選模式返回當(dāng)前所選項(xiàng)目的索引
-
getSelectionModel().getSelectedItem()?- 返回當(dāng)前選定的項(xiàng)目
-
getFocusModel().getFocusedIndex()?- 返回當(dāng)前焦點(diǎn)項(xiàng)的索引
-
getFocusModel().getFocusedItem()?- 返回當(dāng)前關(guān)注的項(xiàng)目
SelectionModel實(shí)例化a時(shí)使用的默認(rèn)值ListView是MultipleSelectionModel抽象類的實(shí)現(xiàn)。但是,selectionMode屬性的默認(rèn)值是SelectionMode.SINGLE。要在默認(rèn)ListView實(shí)例中啟用多個(gè)選擇,請使用以下調(diào)用序列:
listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
另請注意,它MultipleSelectionModel具有selectedItems和selectedIndices屬性,這兩個(gè)屬性都是可觀察的列表,可以監(jiān)視這些列表以檢測任何多個(gè)選擇。
?
使用數(shù)據(jù)填充列表視圖
例11-1顯示了填充列表視圖的最簡單方法。為了提高您的列表,你可以使用的特定擴(kuò)展添加各種類型的數(shù)據(jù)ListCell類,比如CheckBoxListCell,ChoiceBoxListCell,ComboBoxListCell,和TextFieldListCell。這些類為基本列表單元格帶來了額外的功能。為這些類實(shí)現(xiàn)單元工廠使開發(fā)人員能夠直接在列表視圖中更改數(shù)據(jù)。
例如,默認(rèn)情況下,列表單元格的內(nèi)容不可編輯。但是,ComboBoxListCell該類在列表單元格中繪制一個(gè)組合框。此修改使用戶能夠通過從組合框中選擇名稱來構(gòu)建名稱列表,如例11-3所示。
示例11-3將ComboBoxListCell項(xiàng)添加到列表視圖
import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.control.cell.ComboBoxListCell; import javafx.scene.layout.StackPane; import javafx.stage.Stage;public class ListViewSample extends Application {public static final ObservableList names = FXCollections.observableArrayList();public static final ObservableList data = FXCollections.observableArrayList();public static void main(String[] args) {launch(args);}@Overridepublic void start(Stage primaryStage) {primaryStage.setTitle("List View Sample"); final ListView listView = new ListView(data);listView.setPrefSize(200, 250);listView.setEditable(true);names.addAll("Adam", "Alex", "Alfred", "Albert","Brenda", "Connie", "Derek", "Donny", "Lynne", "Myrtle", "Rose", "Rudolph", "Tony", "Trudy", "Williams", "Zach");for (int i = 0; i < 18; i++) {data.add("anonym");}listView.setItems(data);listView.setCellFactory(ComboBoxListCell.forListView(names)); StackPane root = new StackPane();root.getChildren().add(listView);primaryStage.setScene(new Scene(root, 200, 250));primaryStage.show();} }示例中的粗體行調(diào)用該setCellFactory方法重新定義列表單元格的實(shí)現(xiàn)。編譯并運(yùn)行此示例時(shí),它將生成如圖11-4所示的應(yīng)用程序窗口。
圖11-4使用組合框單元格的列表視圖
不僅單元工廠機(jī)制允許您應(yīng)用列表單元格的替代實(shí)現(xiàn),它可以幫助您完全自定義單元格的外觀。
自定義列表視圖的內(nèi)容
研究以下應(yīng)用程序以了解如何使用單元工廠生成列表項(xiàng)。例11-4中顯示的應(yīng)用程序創(chuàng)建了一個(gè)顏色模式列表。
示例11-4創(chuàng)建單元工廠
import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.scene.layout.Priority; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.util.Callback;public class ListViewSample extends Application {ListView<String> list = new ListView<String>();ObservableList<String> data = FXCollections.observableArrayList("chocolate", "salmon", "gold", "coral", "darkorchid","darkgoldenrod", "lightsalmon", "black", "rosybrown", "blue","blueviolet", "brown");@Overridepublic void start(Stage stage) {VBox box = new VBox();Scene scene = new Scene(box, 200, 200);stage.setScene(scene);stage.setTitle("ListViewSample");box.getChildren().addAll(list);VBox.setVgrow(list, Priority.ALWAYS);list.setItems(data);list.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {@Override public ListCell<String> call(ListView<String> list) {return new ColorRectCell();}});stage.show();}static class ColorRectCell extends ListCell<String> {@Overridepublic void updateItem(String item, boolean empty) {super.updateItem(item, empty);Rectangle rect = new Rectangle(100, 20);if (item != null) {rect.setFill(Color.web(item));setGraphic(rect);}}}public static void main(String[] args) {launch(args);} }細(xì)胞工廠生產(chǎn)ListCell物體。每個(gè)單元格都與一個(gè)數(shù)據(jù)項(xiàng)相關(guān)聯(lián),并呈現(xiàn)列表視圖的單個(gè)“行”。單元格通過該setGraphic方法表示的內(nèi)容可以包括其他控件,文本,形狀或圖像。在此應(yīng)用程序中,列表單元格顯示矩形。
編譯并運(yùn)行應(yīng)用程序會生成如圖11-5所示的窗口。
圖11-5顏色模式列表
您可以滾動列表,選擇和取消選擇其任何項(xiàng)目。您還可以擴(kuò)展此應(yīng)用程序以使用顏色模式填充文本標(biāo)簽,如下一節(jié)所示。
?
處理列表項(xiàng)選擇
修改應(yīng)用程序代碼,如例11-5所示,以便在選擇特定列表項(xiàng)時(shí)啟用事件處理。
示例11-5處理列表項(xiàng)的事件
import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.scene.layout.Priority; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.scene.text.Font; import javafx.stage.Stage; import javafx.util.Callback;public class ListViewSample extends Application {ListView<String> list = new ListView<String>();ObservableList<String> data = FXCollections.observableArrayList("chocolate", "salmon", "gold", "coral", "darkorchid","darkgoldenrod", "lightsalmon", "black", "rosybrown", "blue","blueviolet", "brown");final Label label = new Label();@Overridepublic void start(Stage stage) {VBox box = new VBox();Scene scene = new Scene(box, 200, 200);stage.setScene(scene);stage.setTitle("ListViewSample");box.getChildren().addAll(list, label);VBox.setVgrow(list, Priority.ALWAYS);label.setLayoutX(10);label.setLayoutY(115);label.setFont(Font.font("Verdana", 20));list.setItems(data);list.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {@Override public ListCell<String> call(ListView<String> list) {return new ColorRectCell();}});list.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {public void changed(ObservableValue<? extends String> ov, String old_val, String new_val) {label.setText(new_val);label.setTextFill(Color.web(new_val));}});stage.show();}static class ColorRectCell extends ListCell<String> {@Overridepublic void updateItem(String item, boolean empty) {super.updateItem(item, empty);Rectangle rect = new Rectangle(100, 20);if (item != null) {rect.setFill(Color.web(item));setGraphic(rect);}}}public static void main(String[] args) {launch(args);} }addListener調(diào)用的方法selectedItemProperty創(chuàng)建一個(gè)新ChangeListener<String>對象來處理所選項(xiàng)的更改。例如,如果選擇了暗蘭花項(xiàng)目,則標(biāo)簽接收“暗蘭”標(biāo)題并填充相應(yīng)的顏色。修改后的應(yīng)用程序的輸出如圖11-6所示。
圖11-6選擇深色蘭花顏色模式
?
相關(guān)的API文檔 ?
-
ListView
-
ListCell
-
ComboBoxListCell
總結(jié)
以上是生活随笔為你收集整理的JavaFX UI控件教程(十二)之List View的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaFX UI控件教程(十一)之Sc
- 下一篇: JavaFX UI控件教程(十三)之Ta