Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to extend both MouseAdapter and JPanel??

 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it a must to implement MouseAdapter? How about implementing MouseListener instead, and supplying empty implementations for the methods other than mousePressed?
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great!! success!! you are really good, I miss your points, my faults.
sorry,very sorry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic