JDK-8294809 : ListenerHelper for managing and disconnecting listeners
  • Type: Enhancement
  • Component: javafx
  • Sub-Component: controls
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2022-10-04
  • Updated: 2022-12-10
  • Resolved: 2022-11-28
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
openjfx20 b10Fixed
Related Reports
Blocks :  
Blocks :  
Blocks :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
Introduction

There is a number of places where various listeners (strong as well as weak) are added, to be later disconnected in one go.  For example, Skin implementations use dispose() method to clean up the listeners installed in the corresponding Control (sometimes using LambdaMultiplePropertyChangeListenerHandler class).

This pattern is also used by app developers, but there is no public utility class to do that, so every one must invent their own, see for instance
https://github.com/andy-goryachev/FxTextEditor/blob/master/src/goryachev/fx/FxDisconnector.java

Proposal

It might be beneficial to create a class (ListenerHelper, feel free to suggest a better name) which:

- provides convenient methods like addChangeListener, addInvalidationListener, addWeakChangeListener, etc.
- keeps track of the listeners and the corresponding ObservableValues
- provides a single disconnect() method to remove all the listeners in one go.
- optionally, it should be possible to add a lambda (Runnable) to a group of properties
- optionally, there should be a boolean flag to fire the lambda immediately
- strongly suggest implementing an IDisconnectable functional interface with a single disconnect() method

Make it Public Later

Initially, I think we could place the new class in package com.sun.javafx.scene.control; to be made publicly accessible later. 
Comments
Changeset: 3376228a Author: Andy Goryachev <angorya@openjdk.org> Committer: Kevin Rushforth <kcr@openjdk.org> Date: 2022-11-28 16:41:30 +0000 URL: https://git.openjdk.org/jfx/commit/3376228afa0692073d99680558e09bed9c5bfa89
28-11-2022

A pull request was submitted for review. URL: https://git.openjdk.org/jfx/pull/908 Date: 2022-10-07 20:50:55 +0000
12-10-2022