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: 32712
4
posted
0
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: 35258
7
posted
0
Why is it a must to implement MouseAdapter? How about implementing MouseListener instead, and supplying empty implementations for the methods other than mousePressed?
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: 32712
4
posted
0
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: 32712
4
posted
0
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
posted
0
Great!! success!! you are really good, I miss your points, my faults. sorry,very sorry