How to extends both class Rectangle and JComponent?

Dear Friends:I hope to create a class that needs to extends both class Rectangle and JComponent, I know cannot extends TWO diff classes Rectangle JComponent at the same time, but how to do it?Thankssunny
[231 byte] By [sunnymanmana] at [2007-11-26 20:08:15]
# 1

> I hope to create a class that needs to extends both

> class Rectangle and JComponent, I know cannot

> extends TWO diff classes Rectangle JComponent at the

> same time, but how to do it?

Can't be done in Java. There is absolutely no way. If you need this, start learning C++

tjacobs01a at 2007-7-9 23:10:50 > top of Java-index,Desktop,Core GUI APIs...
# 2
So if you know that you can't do it, why are you asking how to do it?
kirillga at 2007-7-9 23:10:51 > top of Java-index,Desktop,Core GUI APIs...
# 3
I urgently need TWO methods each from each class,
sunnymanmana at 2007-7-9 23:10:51 > top of Java-index,Desktop,Core GUI APIs...
# 4
why dont you try a "has-a" relationship for one of them... instead of a "is-a". Then you can write your own methods that correspond to the "has-a" object as needed. As far as the "is-a" (the actually extended object) you just overload what you need.-Js
JSnakea at 2007-7-9 23:10:51 > top of Java-index,Desktop,Core GUI APIs...
# 5
yes, good suggestion, now I can access all these methodsThanks
sunnymanmana at 2007-7-9 23:10:51 > top of Java-index,Desktop,Core GUI APIs...
# 6

Agai, I construct another class as follows:

import javax.swing.*;

import javax.swing.JComponent;

public class JComponentRectangle extends Rectangle{

JComponent jc = null;

TransferHandler th = null;

Image image = null;

public JComponentRectangle(){

}

public TransferHandler setTransferHandler(TransferHandler newHandler){

jc.setTransferHandler(newHandler);

System.out.println("TransferHandler= "+ th);

return newHandler;

}

public Image setImage(Image image2){

image = image2;

System.out.println("image2 = "+ image);

return image;

}

}

but method public TransferHandler setTransferHandler seems not work at all,

what is wrong?

How can I define this setTransferHandler so that it really point to JComponent's setTransferHandler method?

Thanks

sunny

sunnymanmana at 2007-7-9 23:10:51 > top of Java-index,Desktop,Core GUI APIs...
# 7
> Agai, I construct another class as follows:Who's Agai?> jc.setTransferHandler(newHandler);You never set jc to anything. Are you getting a NullPointerException?
kirillga at 2007-7-9 23:10:51 > top of Java-index,Desktop,Core GUI APIs...
# 8

If i'm extending some class and use its method i would do this:

class A extends Point{

A(Point p){

super(p);

}

public double getX(){

int result=super.getX();

System.out.println(""+result);

return result;

}

}

hope you see the pattern there, of course this is only one of the way of doing it, but "super" keyword can be handy sometimes.

Edit: Sorry I misunderstood your ploblem completely. Yeah jc is never set to any value, that's why it's not working.

Message was edited by:

Icycool

Icycoola at 2007-7-9 23:10:51 > top of Java-index,Desktop,Core GUI APIs...
# 9

Thanks, typing error for agai, should be again.

still very confused

for my code:

import javax.swing.*;

import javax.swing.JComponent;

public class JComponentRectangle extends Rectangle{

JComponent jc = null;

TransferHandler th = null;

Image image = null;

public JComponentRectangle(){

}

public TransferHandler setTransferHandler(TransferHandler newHandler){

jc.setTransferHandler(newHandler);

System.out.println("TransferHandler= "+ th);

return newHandler;

}

public Image setImage(Image image2){

image = image2;

System.out.println("image2 = "+ image);

return image;

}

}

I will create 2nd class BBB

public class BBB extends JComponentRectangle(){....}

then I will use third class CCC

public class CCC extends JComponent(){

TransferHandler newHandler = new TransferHandler();

BBB bb = new BBB();

bb.setTransferHandler(newHandler)

}

and I hope bb.setTransferHandler(newHandler) can be available,

you are right, when I run this code, it throws nullpointerException,

How to do it?

please help.

Thanks a lot!!

sunny

sunnymanmana at 2007-7-9 23:10:51 > top of Java-index,Desktop,Core GUI APIs...
# 10
Because JComponent cannot be instantiated,so I set:JComponent jc = null;how to solve above-mentioned problem?thanks a lot, IcyCoolsunny
sunnymanmana at 2007-7-9 23:10:51 > top of Java-index,Desktop,Core GUI APIs...
# 11
Dear Friends:any new idea?Thanks
sunnymanmana at 2007-7-9 23:10:51 > top of Java-index,Desktop,Core GUI APIs...
# 12
So you set it to null and then try to call a method on it. What would you expect to happen? You have to instantiate it to something.
kirillga at 2007-7-9 23:10:51 > top of Java-index,Desktop,Core GUI APIs...
# 13
As I said b4, JComponent cannot be instantiated, If I write JComponent jc = new JComponent ();it cannot compileI have no choice.how to do it?
sunnymanmana at 2007-7-9 23:10:51 > top of Java-index,Desktop,Core GUI APIs...
# 14
Dear Icycool and other guru, any good idea?Thanks
sunnymanmana at 2007-7-9 23:10:51 > top of Java-index,Desktop,Core GUI APIs...
# 15
So when you want to call a method on a JComponent, what is this JComponent? Is this a button in your UI? If it is, just initialize the jc field to that button.
kirillga at 2007-7-21 17:52:00 > top of Java-index,Desktop,Core GUI APIs...
# 16
thanks
sunnymanmana at 2007-7-21 17:52:00 > top of Java-index,Desktop,Core GUI APIs...