*
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: 37958
    
  22
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: 41096
    
  44
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: 37958
    
  22
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: 37958
    
  22
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
 
jQuery in Action, 2nd edition
 
subject: How to extend both MouseAdapter and JPanel??
 
Similar Threads
How to do this??
GridBag help
Best Layout for Simple Forms?
keylistener in a custom component
Forcing size of JPanel