javafx的ListView代入项目的利用

打印 上一主题 下一主题

主题 538|帖子 538|积分 1616

目录
1. 创建一个可观察的列表,用于存储ListView中的数据,这里的User是包装了用户的相关信息。
2.通过本人id获取friendid,及好友的id,然后用集合接送,更方便直观一点。
3.用for遍历集合,逐个添加。
4.渲染器(ImageCellFctoryFriendList)定制
5.渲染用详细方法如下:


1. 创建一个可观察的列表,用于存储ListView中的数据,这里的User是包装了用户的相关信息。

  1. // 创建一个可观察的列表,用于存储ListView中的数据
  2.   ObservableList<User> friendList = FXCollections.observableArrayList();
复制代码
记得把javafx的你需要利用的ListView命名


2.通过本人id获取friendid,及好友的id,然后用集合接送,更方便直观一点。

  1. List<User> ren = (List<User>) Connection.ois.readObject();//我的id,好友id及添加时间
复制代码

3.用for遍历集合,逐个添加。

  1. for (User user : ren) {  
复制代码
sitItems展示我添加的好友信息(项目中我只展示了好友的头像,昵称及在线状态)
  1. this.friendListview.setItems(this.friendList);
  2.                     this.friendList.add(person);
复制代码
最后,通过setCellFctory(渲染器)展示控件的每个单元格,并且它可以答应你为每个单元格提供一个定制的渲染器,这里我定制的渲染器为ImageCellFctoryFriendList(方法名自定义),为自定义函数,格式需要一样,但是内容可以自定义。
  1. this.friendListview.setCellFactory(new ImageCellFactoryFriendList());
复制代码

4.渲染器(ImageCellFctoryFriendList)定制

详细代码在本文章的最后!!!
这里先获取需要的用户信息,然后举行展示,两个50分别为展示头像的长和宽。
  1. //更新单元格内容
  2.                     String username = listviewmember.name;//获取用户名
  3.                     String imagePath = listviewmember.image;//获取用户头像
  4.                     int online = listviewmember.online; // 获取用户在线状态
  5.                     //显示头像
  6.                     File imageFile = new File(imagePath);
  7.                     Image images = new Image(imageFile.toURI().toString());
  8.                     this.imageView.setFitWidth(50.0);
  9.                     this.imageView.setFitHeight(50.0);
  10.                     this.imageView.setImage(images);
  11.                     this.setGraphic(this.imageView);
  12.                     // 设置用户名
  13.                     setText(username);
  14.                     // 设置在线状态的颜色
  15.                     if (online==1) {
  16.                         setTextFill(Color.GREEN); // 在线状态为绿色
  17.                         setText(username + " (在线)");
  18.                     } else {
  19.                         setTextFill(Color.RED); // 不在线状态为红色
  20.                         setText(username + " (离线)");
  21.                     }
  22.                     this.setPrefHeight(-1.0);
复制代码
设置右击菜单,这里右会出现两个按钮,
  1. option1.setOnAction((event) -> {是设置点击按钮1,执行查看资料功能,内容可以直接设置。
复制代码
注意:有几个按钮就需要添加几个进MenuItem。
  1.           //设置右键菜单
  2.                     ContextMenu contextMenu = new ContextMenu();
  3.                     MenuItem option1 = new MenuItem("查看资料");
  4.                     MenuItem option2 = new MenuItem("删除好友");
  5.                     contextMenu.getItems().addAll(new MenuItem[]{option1,option2});
  6.                     this.setContextMenu(contextMenu);
  7.                     //查看资料
  8.                     option1.setOnAction((event) -> {
复制代码
最后显示之前设置的MenuItem。
  1. //设置鼠标点击事件,当右键点击时,显示上述创建的ContextMenu
  2.                     this.setOnMouseClicked((event) -> {
  3.                         if (event.getButton() == MouseButton.SECONDARY) {
  4.                             contextMenu.show(this, event.getScreenX(), event.getScreenY());
  5.                         }
  6.                     });
复制代码
详细效果如下:



5.渲染用详细方法如下:

其中User为用户信息,MarkTool类是为了方便客户端,服务端传递信息的。
  1. public class ImageCellFactoryFriendList implements Callback<ListView<User>, ListCell<User>> {    public ImageCellFactoryFriendList() {    }    public ListCell<User> call(ListView<User> param) {        return new ListCell<User>() {            private ImageView imageView = new ImageView();            protected void updateItem(User listviewmember, boolean empty) {                super.updateItem(listviewmember, empty);                if (!empty && listviewmember != null) {                    //更新单元格内容
  2.                     String username = listviewmember.name;//获取用户名
  3.                     String imagePath = listviewmember.image;//获取用户头像
  4.                     int online = listviewmember.online; // 获取用户在线状态
  5.                     //显示头像
  6.                     File imageFile = new File(imagePath);
  7.                     Image images = new Image(imageFile.toURI().toString());
  8.                     this.imageView.setFitWidth(50.0);
  9.                     this.imageView.setFitHeight(50.0);
  10.                     this.imageView.setImage(images);
  11.                     this.setGraphic(this.imageView);
  12.                     // 设置用户名
  13.                     setText(username);
  14.                     // 设置在线状态的颜色
  15.                     if (online==1) {
  16.                         setTextFill(Color.GREEN); // 在线状态为绿色
  17.                         setText(username + " (在线)");
  18.                     } else {
  19.                         setTextFill(Color.RED); // 不在线状态为红色
  20.                         setText(username + " (离线)");
  21.                     }
  22.                     this.setPrefHeight(-1.0);                    //设置右键菜单
  23.                     ContextMenu contextMenu = new ContextMenu();
  24.                     MenuItem option1 = new MenuItem("查看资料");
  25.                     MenuItem option2 = new MenuItem("删除好友");
  26.                     contextMenu.getItems().addAll(new MenuItem[]{option1,option2});
  27.                     this.setContextMenu(contextMenu);
  28.                     //查看资料
  29.                     option1.setOnAction((event) -> {                        System.out.println("检察资料按钮!!");                        LookPersonalData.id = listviewmember.id;                        LookPersonalData.user=listviewmember;                        FriendPersonalData.user = listviewmember;//???                        URL url = this.getClass().getResource("LookPersonalData.fxml");                        if (url == null) {                            System.err.println("无法找到LookPersonalData.fxml资源文件");                        } else {                            Parent root = null;                            try {                                root = (Parent)FXMLLoader.load(url);                            } catch (IOException var7) {                                IOException e = var7;                                e.printStackTrace();                                return;                            }                            Stage stage = new Stage();                            stage.setTitle("个人界面");                            stage.initStyle(StageStyle.UTILITY);                            Scene scene = new Scene(root, 800.0, 640.0);                            stage.setScene(scene);                            stage.show();                        }                    });                    //删除好友                    option2.setOnAction((event) -> {                        System.out.println("删除好友按钮!!");                           try {                                String id = listviewmember.id;                                String friendid = listviewmember.friendid;                                User u = new User(id, friendid);                                String Operation = MarkTool.DeleteFriend;                                Connection.oos.writeObject(Operation);                                Connection.oos.writeObject(u);                                String response = Connection.ois.readObject().toString();                                System.out.println(response + "删除乐成与否结果已收到");//103    yes                                if (response.equals(MarkTool.DeleteFriendfail)) {                                    Alert alertxx = new Alert(Alert.AlertType.INFORMATION);                                    alertxx.setTitle("错误");                                    alertxx.setHeaderText((String)null);                                    alertxx.setContentText("删除失败,看样子他不想失去你呢!");                                    alertxx.showAndWait();                                }else {                                    Alert alertx = new Alert(Alert.AlertType.INFORMATION);                                    alertx.setTitle("正确");                                    alertx.setHeaderText((String) null);                                    alertx.setContentText("删除乐成,减少一位损友!");                                    alertx.showAndWait();                                }                            }  catch (IOException var15) {                                IOException exx = var15;                                throw new RuntimeException(exx);                            } catch (ClassNotFoundException var16) {                                ClassNotFoundException ex = var16;                                throw new RuntimeException(ex);                            }                    });                    //设置鼠标点击事件,当右键点击时,显示上述创建的ContextMenu
  30.                     this.setOnMouseClicked((event) -> {
  31.                         if (event.getButton() == MouseButton.SECONDARY) {
  32.                             contextMenu.show(this, event.getScreenX(), event.getScreenY());
  33.                         }
  34.                     });                } else {                    this.setText((String)null);                    this.setGraphic((Node)null);                    this.setPrefHeight(0.0);                }            }        };    }}
复制代码

6.打开界面的默认执行函数,显示好友

  1. //发来好友申请集合遍历
  2.     void Friendagrees()throws IOException, ClassNotFoundException{
  3.         this.friendList.clear();
  4.         System.out.println("正在获取好友申请信息");//先通过我的id查询向我添加好友的的人的id
  5.         User er = new User(idself);//j=就一个id
  6.         String Operation = MarkTool.added;//90 yes
  7.         Connection.oos.writeObject(Operation);
  8.         Connection.oos.writeObject(er);
  9.         String response = Connection.ois.readObject().toString();//93 yes
  10.         if (response.equals(MarkTool.returnaddedsuccess)) {
  11.             List<User> ren = (List<User>) Connection.ois.readObject();添加我的人的id,我id,是否同意(状态),添加时间
  12.             System.out.println(ren);
  13.             System.out.println("目前集合ren的个数为:" + ren.size());
  14.             for (User user : ren) {            //遍历user集合,防止多个人添加
  15.                 if (user == null) break;
  16.                 User er1 = new User(user.id);
  17.                 String Operation1 = MarkTool.person;
  18.                 Connection.oos.writeObject(Operation1);//获取指定用户信息
  19.                 Connection.oos.writeObject(er1);
  20.                 String response1 = Connection.ois.readObject().toString();
  21.                 if (response1.equals(MarkTool.returnpersonsuccess)) { //然后获取它的全部信息
  22.                     System.out.println("正在获取id为:" + user.id + "的消息");
  23.                     User person;
  24.                     person = (User) Connection.ois.readObject();
  25.                     person.relation = user.relation;
  26.                     person.time = user.time;
  27.                     person.friendid = idself;
  28. //                    System.out.println("other:" + person.id);
  29. //                    System.out.println("my:" + person.friendid);
  30. //                    System.out.println("签名:" + person.signature);
  31. //                    System.out.println("生日:" + person.birthday);
  32. //                    System.out.println("签名:" + person.name);
  33. //                    System.out.println("性别:" + person.sex);
  34. //                    System.out.println("头像:" + person.image);
  35. //                    System.out.println("邮箱:" + person.email);
  36. //                    System.out.println("申请时间:" + person.time);
  37. //                    System.out.println("申请状态:" + person.relation);
  38.                     LookPersonalData.user = person;//将添加人的信息全部传递给查看他人资料界面
  39.                     this.friendlistveiw.setItems(this.friendList);
  40.                     this.friendList.add(person);
  41.                 }
  42.                 System.out.println(user.id + "正在加入集合");
  43.             }
  44.             System.out.println("集合List遍历完毕");
  45.             this.friendlistveiw.setCellFactory(new ImageCellFactorytz());
  46.         }
  47.     }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

南七星之家

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表