?
Solved

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

Posted on 2014-11-13
3
Medium Priority
?
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
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 …
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

800 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