当前位置:   article > 正文

javafx的ListView代入项目的使用

javafx的ListView代入项目的使用

目录

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遍历集合,逐个添加。

for (User user : ren) {  

sitItems展示我添加的好友信息(项目中我只展示了好友的头像,昵称及在线状态)

  1. this.friendListview.setItems(this.friendList);
  2. this.friendList.add(person);

最后,通过setCellFctory渲染器)展示控件的每个单元格,并且它可以允许你为每个单元格提供一个定制的渲染器,这里我定制的渲染器为ImageCellFctoryFriendList(方法名自定义),为自定义函数,格式需要一样,但是内容可以自定义。

 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);

设置右击菜单,这里右会出现两个按钮,

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>> {
  2. public ImageCellFactoryFriendList() {
  3. }
  4. public ListCell<User> call(ListView<User> param) {
  5. return new ListCell<User>() {
  6. private ImageView imageView = new ImageView();
  7. protected void updateItem(User listviewmember, boolean empty) {
  8. super.updateItem(listviewmember, empty);
  9. if (!empty && listviewmember != null) {
  10. //更新单元格内容
  11. String username = listviewmember.name;//获取用户名
  12. String imagePath = listviewmember.image;//获取用户头像
  13. int online = listviewmember.online; // 获取用户在线状态
  14. //显示头像
  15. File imageFile = new File(imagePath);
  16. Image images = new Image(imageFile.toURI().toString());
  17. this.imageView.setFitWidth(50.0);
  18. this.imageView.setFitHeight(50.0);
  19. this.imageView.setImage(images);
  20. this.setGraphic(this.imageView);
  21. // 设置用户名
  22. setText(username);
  23. // 设置在线状态的颜色
  24. if (online==1) {
  25. setTextFill(Color.GREEN); // 在线状态为绿色
  26. setText(username + " (在线)");
  27. } else {
  28. setTextFill(Color.RED); // 不在线状态为红色
  29. setText(username + " (离线)");
  30. }
  31. this.setPrefHeight(-1.0);
  32. //设置右键菜单
  33. ContextMenu contextMenu = new ContextMenu();
  34. MenuItem option1 = new MenuItem("查看资料");
  35. MenuItem option2 = new MenuItem("删除好友");
  36. contextMenu.getItems().addAll(new MenuItem[]{option1,option2});
  37. this.setContextMenu(contextMenu);
  38. //查看资料
  39. option1.setOnAction((event) -> {
  40. System.out.println("查看资料按钮!!");
  41. LookPersonalData.id = listviewmember.id;
  42. LookPersonalData.user=listviewmember;
  43. FriendPersonalData.user = listviewmember;//???
  44. URL url = this.getClass().getResource("LookPersonalData.fxml");
  45. if (url == null) {
  46. System.err.println("无法找到LookPersonalData.fxml资源文件");
  47. } else {
  48. Parent root = null;
  49. try {
  50. root = (Parent)FXMLLoader.load(url);
  51. } catch (IOException var7) {
  52. IOException e = var7;
  53. e.printStackTrace();
  54. return;
  55. }
  56. Stage stage = new Stage();
  57. stage.setTitle("个人界面");
  58. stage.initStyle(StageStyle.UTILITY);
  59. Scene scene = new Scene(root, 800.0, 640.0);
  60. stage.setScene(scene);
  61. stage.show();
  62. }
  63. });
  64. //删除好友
  65. option2.setOnAction((event) -> {
  66. System.out.println("删除好友按钮!!");
  67. try {
  68. String id = listviewmember.id;
  69. String friendid = listviewmember.friendid;
  70. User u = new User(id, friendid);
  71. String Operation = MarkTool.DeleteFriend;
  72. Connection.oos.writeObject(Operation);
  73. Connection.oos.writeObject(u);
  74. String response = Connection.ois.readObject().toString();
  75. System.out.println(response + "删除成功与否结果已收到");//103 yes
  76. if (response.equals(MarkTool.DeleteFriendfail)) {
  77. Alert alertxx = new Alert(Alert.AlertType.INFORMATION);
  78. alertxx.setTitle("错误");
  79. alertxx.setHeaderText((String)null);
  80. alertxx.setContentText("删除失败,看样子他不想失去你呢!");
  81. alertxx.showAndWait();
  82. }else {
  83. Alert alertx = new Alert(Alert.AlertType.INFORMATION);
  84. alertx.setTitle("正确");
  85. alertx.setHeaderText((String) null);
  86. alertx.setContentText("删除成功,减少一位损友!");
  87. alertx.showAndWait();
  88. }
  89. } catch (IOException var15) {
  90. IOException exx = var15;
  91. throw new RuntimeException(exx);
  92. } catch (ClassNotFoundException var16) {
  93. ClassNotFoundException ex = var16;
  94. throw new RuntimeException(ex);
  95. }
  96. });
  97. //设置鼠标点击事件,当右键点击时,显示上述创建的ContextMenu
  98. this.setOnMouseClicked((event) -> {
  99. if (event.getButton() == MouseButton.SECONDARY) {
  100. contextMenu.show(this, event.getScreenX(), event.getScreenY());
  101. }
  102. });
  103. } else {
  104. this.setText((String)null);
  105. this.setGraphic((Node)null);
  106. this.setPrefHeight(0.0);
  107. }
  108. }
  109. };
  110. }
  111. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/886048
推荐阅读
相关标签
  

闽ICP备14008679号