Solved

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

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

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

726 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