package listDemo;import javafx.application.Application;import javafx.collections.FXCollections;import javafx.collections.ObservableList;import javafx.scene.Parent;import javafx.scene.Scene;import javafx.scene.control.ListCell;import javafx.scene.control.ListView;import javafx.scene.layout.BorderPane;import javafx.stage.Stage;import javafx.util.Callback;/*** List view 显示* @author RiverLei* @Date 2021年08月02日* @Description: TODO* @Version 1.0*/public class ListViewDemo extends Application {public static void main(String[] args) {launch(args);}//创建ListView 指定数据项选择ListView<Student> listView=new ListView<>();//数据源ObservableList<Student> listData=FXCollections.observableArrayList();@Overridepublic void start(Stage primaryStage) throws Exception {listData.add(new Student(1,"shao",true));listData.add(new Student(2,"wang",true));listData.add(new Student(3,"jiang",true));listView.setItems(listData);//设置单元格生成器listView.setCellFactory(new Callback<ListView<Student>, ListCell<Student>>() {@Overridepublic ListCell<Student> call(ListView<Student> param) {return new MyListCell();}});BorderPane borderPane=new BorderPane();borderPane.setCenter(listView);primaryStage.setScene(new Scene(borderPane,400,400));primaryStage.show();}//负责单元格cell的显示static class MyListCell extends ListCell<Student>{@Overrideprotected void updateItem(Student item, boolean empty) {//Fx框架 要求必须先diaoyongsuper.updateItem(item, empty);if(item!=null){this.setText(item.name+"-"+item.id);}}}static class Student{public int id;public String name;public boolean sex;public Student(){};public Student(int id,String name,boolean sex){this.id=id;this.name=name;this.sex=sex;}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。