Solved

Is there a standard Java interface for "implements addActionListener", "implements addChangeListener", etc.?

Posted on 2014-11-13
3
144 Views
Last Modified: 2014-11-13
Is there a standard Java interface for "implements addActionListener", "implements addChangeListener", "implements AddPropertyChangeListener"?

Say I create a new component, and this new component has a JButton and let's say a JSpinner configured to show numeric values, the JButton creates an Action, and the JSpinner is a property who's value changes, so I expect the user to call addActionListener to know when the button is pressed, and addPropertyChangeListener to know when the value of the JSpinner has changed.

Are there standard interfaces I should have my component implement that says, "This component supports addActionListener", and "This component supports addPropertyChangeListener"?

If the answer is "No", which I suspect is the case, then my next question is, "Why not?"

When I look at a class and I want to know what calls this class supports, I look at the interfaces. If there's no interface telling me "This class supports addActionListener", then it seems like I'm just supposed to magically "know" this (perhaps by reading the unwritten documentation?)

So, where am I going wrong in my thinking here?
0
Comment
Question by:deleyd
  • 2
3 Comments
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
So, where am I going wrong in my thinking here?
You're going wrong in thinking that the documentation is "unwritten." Here's the API documentation for JButton:

https://docs.oracle.com/javase/8/docs/api/javax/swing/JButton.html

It shows that addActionListener() is being inherited from AbstractButton. From that, you now know that everything that extends AbstractButton will support that addActionListener() call.

You ask, "How am I supposed to know what classes support addActionListener()?" Well, do you know you're supposed to use addActionListener() in the first place? You learned it when you started learning Swing, right? It's just part of what you have to learn when you learn how to use the JButton class.
0
 

Author Comment

by:deleyd
Comment Utility
So my error in thinking is looking at the interfaces to see what is implemented?

The "Unwritten documentation" I was referring to is the documentation not yet written for the new sample component I proposed that has a JButton and a JSpinner combined. I guess I just add comments to the top of the file for my new component explaining that it supports addActionListener for the JButton, and addPropertyChangeListener for the JSpinner?

(Or I just expect other programmers to wade through the code to discover what calls are available and figure it out for themselves?)
0
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
Comment Utility
If I encountered an unfamiliar component, I would expect that the Javadocs for that class would tell me how to use it, so adding that information to the class comment is probably the way to go.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now