Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem in overriding add() !

3 views
Skip to first unread message

A.B.VijayKumar

unread,
Apr 5, 1997, 3:00:00 AM4/5/97
to

Hi,
I have inherited applet and trying to override add() method to add the
component to a panel in the applet.

I get the message
------------------------------------------------------------------------

-- java.lang.IllegalArgumentException: adding container's parent to
itself
at java.awt.Container.add(Container.java:120)
at java.awt.Container.add(Container.java:102)
at ScrollPanel.add(Compiled Code)
at java.awt.Container.add(Container.java:161)
at ScrollPanel.<init>(Compiled Code)
at ScrollApplet.<init>(Compiled Code)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:386)
at sun.applet.AppletPanel.run(AppletPanel.java:204)
at java.lang.Thread.run(Thread.java)
------------------------------------------------------------------------

Here is the add() method
-----------------------------------------------------------------
public Component add( Component c)
{
Mypanel.add(c);
return c;
}
------------------------------------------------------
and in inherintg class I give
-=-----------------------
. .. . . .
add(new Button("hello");
------------

What is the mistake
thanks in advance
vijju


Have A Nice Time ......
--------------------------------------------------------
e-mail vi...@regal.india.hp.com
------------------------ ) (
A. B. Vijay Kumar // ~~~~~~~~~~ \\
Hewlett Packard ISO \\ / \ //
30c Cunningham Road \| O O |/
Bangalore 560 052 \ /
\ (o o) /
Phone 2251554 x 1466 \ ~~~~ /
----------------------------o0o--------------------o0o---
) ) )
---------------------------------------------------------

Jim Balter

unread,
Apr 5, 1997, 3:00:00 AM4/5/97
to

A.B.VijayKumar wrote:
>
> Hi,
> I have inherited applet and trying to override add() method to add the
> component to a panel in the applet.

Container (which your Applet is an instance of) knows how to add
Components; you do not want to override add unless you want to change
how to add or what happens when one adds. This is basic OOP philosophy
stuff that I can't cover here, but consider reading up on OOP
techniques. On to specifics:

> I get the message
> ------------------------------------------------------------------------
>
> -- java.lang.IllegalArgumentException: adding container's parent to
> itself
> at java.awt.Container.add(Container.java:120)
> at java.awt.Container.add(Container.java:102)
> at ScrollPanel.add(Compiled Code)
> at java.awt.Container.add(Container.java:161)
> at ScrollPanel.<init>(Compiled Code)
> at ScrollApplet.<init>(Compiled Code)
> at sun.applet.AppletPanel.runLoader(AppletPanel.java:386)
> at sun.applet.AppletPanel.run(AppletPanel.java:204)
> at java.lang.Thread.run(Thread.java)
> ------------------------------------------------------------------------
>
> Here is the add() method
> -----------------------------------------------------------------
> public Component add( Component c)
> {
> Mypanel.add(c);

You get the message when you add Mypanel (you should use names
starting with upper case for classes and interfaces only, not for
variables; see a Java tutorial) to your applet. That invokes
this overridden add method, which tries to add Mypanel to itself.
Remove this add method altogether.

> return c;
> }
> ------------------------------------------------------
> and in inherintg class I give
> -=-----------------------
> . .. . . .
> add(new Button("hello");

Just use
myPanel.add(new Button("hello"));

(note "corrected" spelling of Mypanel)

or, with more proper OOP technique, add the Components of myPanel
in myPanel, not in the applet that contains it. This is best done
in the addNotify() method (that's a long story, covered in some of
my other postings), like so:


class myPanel
{
...

public void addNotify()
{
super.addNotify(); // don't forget this!!

add(new Button("hello"));
...
}

...
}

--
<J Q B>

Gordon Keith

unread,
Apr 8, 1997, 3:00:00 AM4/8/97
to

In article <334627...@regal.india.hp.com>, "A.B.VijayKumar" <vi...@regal.india.hp.com> wrote:
>Hi,
> I have inherited applet and trying to override add() method to add the
>component to a panel in the applet.
>
> I get the message
>------------------------------------------------------------------------
>
>-- java.lang.IllegalArgumentException: adding container's parent to
>itself

> Here is the add() method


>-----------------------------------------------------------------
>public Component add( Component c)
>{
> Mypanel.add(c);

> return c;
>}

How do you add Mypanel to your applet?

If you are not doing:
super.add(Mypanel);
then you are probably trying to Mypanel.add(Mypanel) which isn't kosha.

Hope this helps.
Gordon

-- gordo...@antdiv.gov.au --- gordo...@acslink.net.au
Gordon Keith ,-_|\ Ph: +61 3 6232 3489
Programmer / \ Fax: +61 3 6232 3351
Marine Science Section \_,-._/
Australian Antarctic Division *
http://www.antdiv.gov.au http://acslink.net.au/~gordonkeith
(Standard disclaimer applies)

~!

0 new messages