当前位置: 首页 > news >正文

javafx的ListView代入项目的使用

目录

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

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

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

4.渲染器(ImageCellFctoryFriendList)定制

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


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

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

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

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

​List<User> ren = (List<User>) 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。

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

具体效果如下:

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

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


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) {//更新单元格内容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    yesif (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);}});//设置鼠标点击事件,当右键点击时,显示上述创建的ContextMenuthis.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);}}};}
}

http://www.lryc.cn/news/406871.html

相关文章:

  • 基于ABAP OLE技术实现对服务器文件进行读写操作
  • 求教Postgresql在jdbc处理bit(1)字段的预处理解决方案
  • 微信小程序-自定义tabBar
  • vue3+element-plus 实现动态菜单和动态路由的渲染
  • GO-学习-03-基本数据类型
  • 高并发场景下,系统的保护机制
  • 服务器构建私有npm库(Docker + Verdaccio)
  • LabVIEW做二次开发时应该注意哪些方面?
  • docker配置上网代理获取镜像
  • SqlSugar删除没有定义主键的实体类对应的数据库表数据
  • 虚拟机复制后网络不可用,报错“network.service - LSB: Bring up/down networking”
  • Redis 7.x 系列【30】集群管理命令
  • 将YOLOv8模型从PyTorch的.pt格式转换为TensorRT的.engine格式
  • Hello SLAM(在Linux中实现第一个C++程序)
  • IPD推行成功的核心要素(十五)项目管理提升IPD相关项目交付效率和用户体验
  • C++ 鼠标轨迹API【神诺科技SDK】
  • 设计模式|观察者模式
  • python自动化运维 通过paramiko库和time库实现服务器自动化管理
  • HTML常用的转义字符——怎么在网页中写“<div></div>”?
  • shell-awk文本处理工具
  • 如何在测试中保护用户隐私!
  • ARCGIS PRO DSK GraphicsLayer创建文本要素
  • 看板项目之vue代码分析
  • lua 游戏架构 之 游戏 AI (七)ai_dead
  • 前端开发知识(一)-html
  • 身份证如何查验真伪?C#身份证二要素、三要素接口集成
  • C++ | Leetcode C++题解之第290题单词规律
  • Pytorch使用教学7-张量的广播
  • 生成式AI:对话系统(Chat)与自主代理(Agent)的和谐共舞
  • 唯众物联网(IOT)全功能综合实训教学解决方案