This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes How to extend both MouseAdapter  and JPanel?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to extend both MouseAdapter  and JPanel??" Watch "How to extend both MouseAdapter  and JPanel??" New topic
Author

How to extend both MouseAdapter and JPanel??

Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
Dear friends:
I know java can only extend one class, so looks like I issue an impossible question, but I need to solve this.
I have following code, The JLabels here can be clickable/selectable and deletable, it extends MouseAdapter class and it is a must. but my project needs to extend JPanel, it is also a must, how can I reach this?? I tried to create an inner class to extend MouseAdapter, then to make JLabel clickable/selectable and deletable, like this code. but fails.
can any guru help? or throw some light?


Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
You need to change two words.
Alter "extends MouseAdapter" to "implements MouseListener."
You will then have to find the remaining 4 methods of MouseListener either from the API documentation, or from the error messages when it won't compile.

Then you can add "extends JPanel" beforee "implements".

Easy
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42030
    
  64
Why is it a must to implement MouseAdapter? How about implementing MouseListener instead, and supplying empty implementations for the methods other than mousePressed?


Ping & DNS - my free Android networking tools app
Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
Originally posted by Campbell Ritchie:
You need to change two words.
Alter "extends MouseAdapter" to "implements MouseListener."
You will then have to find the remaining 4 methods of MouseListener either from the API documentation, or from the error messages when it won't compile.

Then you can add "extends JPanel" beforee "implements".

Easy


Do you mean
"public class DeletingLabels extends JPanel implements MouseAdapter {..."??

If I didnot remember wrongly, class cannot be imepemented, only extended.
MouseListener is a class, not interface.

If your idea is correct, can you provide runnable code to prove it??
Thanks
[ May 25, 2007: Message edited by: girl sunny ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Yes. As for runnable code, copy and paste your mousePressed method. Go and find the other four methods from where I told you and implement them with bodies like this
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
I said "implements MouseListener" not "implements MouseAdapter". Go and have a look in the API where I told you and also the Java Tutorial about interfaces.
Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
Great!! success!! you are really good, I miss your points, my faults.
sorry,very sorry
 
GeeCON Prague 2014
 
subject: How to extend both MouseAdapter and JPanel??