• Post Reply Bookmark Topic Watch Topic
  • New Topic

generic inheritance  RSS feed

 
Vasco Carmona
Greenhorn
Posts: 13
Firefox Browser Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I tried to find this information but I didn't found it.

I would like to make an abstract class called ComponentMouseAdapter that extends any subclass of javax.swing.JComponent and implements all mouse listeners.

Is it possible to have generic inheritance?

I thought that the code would be something like this:


thanks in advance to all replies.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sorry, Java doesn't have such a feature.... perhaps it would be good to explain what you are trying to do, so that we can suggest an alternative?

Henry
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe ComponentMouseAdapter<E extends MouseListener>??

But there is already a mouseAdapter class and a MouseMotionAdapter class. Why do you need to duplicate them?
 
Vasco Carmona
Greenhorn
Posts: 13
Firefox Browser Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Henry,

thanks for the reply.

I'm building an application that adds labels with images and/or text to a panel at runtime, drags them to wherever we want and change some properties.

For now I just need to insert labels but I'm thinking ahead and trying to do a "universal" inserter for all JComponent subclasses, and all of them to be "dragable" and "changeable". For that I made an interface that as a method called getElement() that return a generic subclass of JComponent.
I could extend the mouse listeners on this interface but I don't want to be able to access directly the mouse listeners methods through objects that implements that interface.
Thus, I wanted to make an abstract class that could extends any subclass of JComponent and make some kind of MouseAdapter, and those that extended this abstract class would override the methods that were needed.

Since it's not possible, I'll make a class that extends JLabel, or any other subclass of JComponent, that implements all those interfaces.

thanks again for the reply.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like what is being attempted is creating components (of arbitrary type) that already have built-in mouse event handling.

This doesn't, to me, seem like a problem that is best solved using inheritance. I'd prefer to use composition. If there really is a need to have the same mouse-event handling on arbitrary JComponents, then it would be simple to inherit from MouseAdapter/MouseMotionAdapter, give the class a reference to a JComponent (passed via the constructor), and then use that reference. Then you just create an instance of this class attached to your component whenever you need it. No generics required.

Edit: I wrote that before I saw Vasco's response. I'd still say the same thing, though.
 
Vasco Carmona
Greenhorn
Posts: 13
Firefox Browser Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I need is an object that "is-a" JLabel with additional properties.
Since I can only extend from one class, I extend from JLabel and implement the mouse listeners to make my own mouse adapter.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!