<AWT Dev> RFE: Listeners with Adapter-classes should have empty default methods

Anthony Petrov anthony.petrov at oracle.com
Mon Aug 25 10:25:39 UTC 2014


Hi Andreas,

Yes, in fact Petr has proposed this same enhancement back in January:

http://mail.openjdk.java.net/pipermail/awt-dev/2014-January/006799.html

Do you want to contribute a patch for this RFE? If so, please read this 
document:

http://openjdk.java.net/contribute/

The most important part is to get your OCA signed, after which we can 
accept patches from you.

--
best regards,
Anthony

On 8/24/2014 3:42 AM, Andreas Lundblad wrote:
> For listener interfaces where it's common to just implement one or two
> methods, there are corresponding Adapter classes with empty method
> implementations. (For example: MouseListener/MouseAdapter,
> KeyListener/KeyAdapter, ComponentListener/ComponentAdapter, ...)
>
> The Adapter classes are really convenient for obvious reasons. However,
> due to the constraint of single inheritance, they can't be used together
> with each other. That is, if I want to listen to key presses
> (KeyListener.keyPressed) and mouse clicks (MouseListener.mouseClicked)
> I'm doomed to either
>
>   1) choose to extend one of the adapters and clutter my code with empty
> method implementations for the other, or
>
>   2) Extend the two adapters in two separate classes and instantiate two
> separate listener objects (which gets slightly messy if they need to
> share some state).
>
> Therefor I propose to add empty default implementations for these types
> of listener interfaces. This would allow me to for instance implement
> KeyListener and MouseListener and just override keyPressed and mouseClicked.
>
> Some basic grepping yields the following list:
>
> ComponentListener
> ContainerListener
> DragSourceListener
> DropTargetListener
> FocusListener
> HierarchyBoundsListener
> InternalFrameListener
> KeyListener
> MouseListener
> MouseMotionListener
> PrintJobListener
> WindowListener
>
> best regards,
> Andreas Lundblad


More information about the awt-dev mailing list