Solved

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

Posted on 2014-11-13
3
150 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
ID: 40440342
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
ID: 40440526
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
ID: 40440569
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java - Why doesn't this JFrame work 3 58
Why method in Java which is called from Runnable run() doesn't need to be 'static'? 1 24
table example 4 29
runtime exception 2 27
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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 …

776 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