javafx的ListView代入项目的使用
创始人
2024-12-01 06:35:20
0

目录

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

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

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

4.渲染器(ImageCellFctoryFriendList)定制

5.渲染器具体方法如下:


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

 // 创建一个可观察的列表,用于存储ListView中的数据   ObservableList friendList = FXCollections.observableArrayList();

记得把javafx的你需要使用的ListView命名

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

​  List ren = (List) Connection.ois.readObject();//我的id,好友id及添加时间  ​

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

for (User user : ren) {  

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

​  this.friendListview.setItems(this.friendList);                     this.friendList.add(person);  ​

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

 this.friendListview.setCellFactory(new ImageCellFactoryFriendList());

4.渲染器(ImageCellFctoryFriendList)定制

具体代码在本文章的最后!!!

这里先获取需要的用户信息,然后进行展示,两个50分别为展示头像的长和宽。

 //更新单元格内容                     String username = listviewmember.name;//获取用户名                     String imagePath = listviewmember.image;//获取用户头像                     int online = listviewmember.online; // 获取用户在线状态                     //显示头像                     File imageFile = new File(imagePath);                     Image images = new Image(imageFile.toURI().toString());                     this.imageView.setFitWidth(50.0);                     this.imageView.setFitHeight(50.0);                     this.imageView.setImage(images);                     this.setGraphic(this.imageView);                     // 设置用户名                     setText(username);                     // 设置在线状态的颜色                     if (online==1) {                         setTextFill(Color.GREEN); // 在线状态为绿色                         setText(username + " (在线)");                     } else {                         setTextFill(Color.RED); // 不在线状态为红色                         setText(username + " (离线)");                     }                     this.setPrefHeight(-1.0);

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

option1.setOnAction((event) -> {是设置点击按钮1,执行查看资料功能,内容可以直接设置。

注意:有几个按钮就需要添加几个进MenuItem。

          //设置右键菜单                     ContextMenu contextMenu = new ContextMenu();                     MenuItem option1 = new MenuItem("查看资料");                     MenuItem option2 = new MenuItem("删除好友");                     contextMenu.getItems().addAll(new MenuItem[]{option1,option2});                     this.setContextMenu(contextMenu);                     //查看资料                     option1.setOnAction((event) -> {

最后显示之前设置的MenuItem。

//设置鼠标点击事件,当右键点击时,显示上述创建的ContextMenu                     this.setOnMouseClicked((event) -> {                         if (event.getButton() == MouseButton.SECONDARY) {                             contextMenu.show(this, event.getScreenX(), event.getScreenY());                         }                     });

具体效果如下:

5.渲染器具体方法如下:

其中User为用户信息,MarkTool类是为了方便客户端,服务端传递信息的。

 public class ImageCellFactoryFriendList implements Callback, ListCell> {     public ImageCellFactoryFriendList() {     }      public ListCell call(ListView param) {         return new ListCell() {             private ImageView imageView = new ImageView();             protected void updateItem(User listviewmember, boolean empty) {                 super.updateItem(listviewmember, empty);                 if (!empty && listviewmember != null) {                     //更新单元格内容                     String username = listviewmember.name;//获取用户名                     String imagePath = listviewmember.image;//获取用户头像                     int online = listviewmember.online; // 获取用户在线状态                     //显示头像                     File imageFile = new File(imagePath);                     Image images = new Image(imageFile.toURI().toString());                     this.imageView.setFitWidth(50.0);                     this.imageView.setFitHeight(50.0);                     this.imageView.setImage(images);                     this.setGraphic(this.imageView);                     // 设置用户名                     setText(username);                     // 设置在线状态的颜色                     if (online==1) {                         setTextFill(Color.GREEN); // 在线状态为绿色                         setText(username + " (在线)");                     } else {                         setTextFill(Color.RED); // 不在线状态为红色                         setText(username + " (离线)");                     }                     this.setPrefHeight(-1.0);                     //设置右键菜单                     ContextMenu contextMenu = new ContextMenu();                     MenuItem option1 = new MenuItem("查看资料");                     MenuItem option2 = new MenuItem("删除好友");                     contextMenu.getItems().addAll(new MenuItem[]{option1,option2});                     this.setContextMenu(contextMenu);                     //查看资料                     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                     this.setOnMouseClicked((event) -> {                         if (event.getButton() == MouseButton.SECONDARY) {                             contextMenu.show(this, event.getScreenX(), event.getScreenY());                         }                     });                 } else {                     this.setText((String)null);                     this.setGraphic((Node)null);                     this.setPrefHeight(0.0);                 }              }         };     } }

相关内容

热门资讯

总算了解!广西老友有破解吗,开... 总算了解!广西老友有破解吗,开心泉州免费辅助器,攻略教程(有挂分享)1、开心泉州免费辅助器有没有辅助...
无独有偶!微信小程序免费黑科技... 无独有偶!微信小程序免费黑科技透视,微乐小程序多功能修改器法子教程(有挂分享)1、这是跨平台的微乐小...
避坑细节!宝宝游戏辅助,决战卡... 避坑细节!宝宝游戏辅助,决战卡五星游戏辅助器,积累教程(有挂规律)亲,关键说明,决战卡五星游戏辅助器...
据权威媒体报道!微信小程序免费... 据权威媒体报道!微信小程序免费黑科技透视,钱塘十三水其实真的有挂手段教程(有挂详细)1、钱塘十三水其...
实测揭晓!同乡游辅助工具制作,... 实测揭晓!同乡游辅助工具制作,永胜联盟会封号吗,模块教程(有挂教学)1、同乡游辅助工具制作模拟器是什...
实测必看!微信小程序免费黑科技... 实测必看!微信小程序免费黑科技开挂,皮皮胡子辅助举措教程(有挂详细)1、起透看视 微信小程序免费黑科...
一分钟揭秘!微乐小程序免费黑科... 一分钟揭秘!微乐小程序免费黑科技安卓,新超凡软件辅助练习教程(有挂存在)1、微乐小程序免费黑科技脚本...
必备辅助推荐!蜜瓜大厅可以装挂... 必备辅助推荐!蜜瓜大厅可以装挂吗,友友联盟辅助脚本,指南书教程(有挂技术)1、进入游戏-大厅左侧-新...
今日焦点!微信小程序免费黑科技... 今日焦点!微信小程序免费黑科技透视,川南久久辅助模块教程(存在有挂)运微信小程序免费黑科技辅助工具,...
技术分享!山西扣点点辅助工具,... 技术分享!山西扣点点辅助工具,福州十八扑外卦视频,诀窍教程(有挂方法)1、山西扣点点辅助工具公共底牌...