NCN*_*ros 6 java user-interface javafx javafx-8
我如何使用JavaFx为我的应用程序制作自定义ListView.我需要每个行listView的HBox图像和2个标签.
Ada*_*dam 10
您需要通过ListView.setCellFactory(...)提供自定义CellFactory

工作实例
public class CustomListView extends Application {
private static class CustomThing {
private String name;
private int price;
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public CustomThing(String name, int price) {
super();
this.name = name;
this.price = price;
}
}
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
ObservableList<CustomThing> data = FXCollections.observableArrayList();
data.addAll(new CustomThing("Cheese", 123), new CustomThing("Horse", 456), new CustomThing("Jam", 789));
final ListView<CustomThing> listView = new ListView<CustomThing>(data);
listView.setCellFactory(new Callback<ListView<CustomThing>, ListCell<CustomThing>>() {
@Override
public ListCell<CustomThing> call(ListView<CustomThing> listView) {
return new CustomListCell();
}
});
StackPane root = new StackPane();
root.getChildren().add(listView);
primaryStage.setScene(new Scene(root, 200, 250));
primaryStage.show();
}
private class CustomListCell extends ListCell<CustomThing> {
private HBox content;
private Text name;
private Text price;
public CustomListCell() {
super();
name = new Text();
price = new Text();
VBox vBox = new VBox(name, price);
content = new HBox(new Label("[Graphic]"), vBox);
content.setSpacing(10);
}
@Override
protected void updateItem(CustomThing item, boolean empty) {
super.updateItem(item, empty);
if (item != null && !empty) { // <== test for null item and empty parameter
name.setText(item.getName());
price.setText(String.format("%d $", item.getPrice()));
setGraphic(content);
} else {
setGraphic(null);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)