Solved

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

Posted on 2014-11-13
3
153 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

792 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