javafx-在listview中添加了点击事件后会执行多次

现象

我在一个按钮的点击事件中添加了,点击事件的监听如下,就会造成我多次点击按钮后,里面打开浏览器会执行多次

java 复制代码
  listviewitemListener = new ChangeListener() {
      @Override
      public void changed(ObservableValue observable, Object oldValue, Object newValue) {
          int start = 0;
          String numStr = null;
          String valuesStr = newValue.toString();
          String[] strArray = valuesStr.split(":");
          valuesStr = strArray[1];
          numStr = valuesStr.substring(0, 8);
          if (Integer.valueOf(numStr) > 0) {
              String finalNumStr = numStr;
              Platform.runLater(new Runnable() {
                  @Override
                  public void run() {
                      String path = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";
                      String target = "https://xxxx=" + finalNumStr;
                      List<String> cmd = new ArrayList<String>();
                      cmd.add(path);
//                            cmd.add("--start-maximized");// 窗口启动最大化
//                            cmd.add("--incognito");// 隐身模式打开
//                            cmd.add("--user-data-dir=D:/test");
                      cmd.add(target);
                      ProcessBuilder process = new ProcessBuilder(cmd);
                      try {
                          process.start();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              });
          }
      }
  };

  infoLV.getSelectionModel().selectedItemProperty().addListener(listviewitemListener);

解决方法

先把listener设置为类的属性,开始设置为null,然后在按钮点击的时候判断不为null的时候remove这个listener就可以了

java 复制代码
private ChangeListener listviewitemListener=null;


@FXML
void check(ActionEvent event)  {
        if(listviewitemListener!=null){
            infoLV.getSelectionModel().selectedItemProperty().removeListener(listviewitemListener);
        }
}
相关推荐
金銀銅鐵2 小时前
用 Swing 重做操作数栈演示
java·jvm·后端
琳少_2 小时前
java程序开发笔记
java·开发语言·数据库·笔记
叼烟扛炮4 小时前
[LangChain] 学习笔记(15):为什么你的重试没用——429、抖动、fallback 与熔断
java·服务器·前端
文丑颜不良啊5 小时前
LeetCode3401~3420题目解析
java·leetcode
文丑颜不良啊6 小时前
LeetCode3421~3440题目解析
java·leetcode
code2cat7 小时前
【随笔】Java客户端选了HTTP/3,怎样知道请求实际走了哪个版本
java·开发语言·http
码云社区9 小时前
Java在线租房系统源码:VR看房素材、房源图片与内容维护方案
java·vue.js·vr·租房小程序·在线租房·看房租房
码云社区9 小时前
Spring Boot智慧物业系统源码:合同到期提醒、续签资料与资产协作
java·spring boot·后端·物业管理·物业系统·智慧物业
AI开源软件9 小时前
Vue3+SpringBoot3 业务配置统一管理:接管、保存与回退怎样保留有效值
java·前端·vue.js·spring boot·vue·ruoyi office
陈如水10 小时前
Spring AI Alibaba 在多 Agent 场景下的实践
java·人工智能·spring