Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When component class add method will throws an IllegalArguemnt Exception?

 
sun ram
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Container methods add( Component comp ) and add( String name, Component comp ) will throw an IllegalArgumentException if comp is a:
a) button
b) list
c) window
d) textarea
e) container that contains this container
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer:c,e
If you have a quick look at the source of java.awt.Container you'll see that. I admit that it could be mentioned somewhere in the API...
[ February 07, 2002: Message edited by: Valentin Crettaz ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valentin,
I'm pretty sure you just made a simple typo above, because the answers are c & e, NOT c & d as you wrote.
It's perfectly legal to add a textarea to a container, but it's an error to try to add the container's parent to the container.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob,
Damn it, typo again
I confirm answers are c and e
I've edited my previous post
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Val for reminder:
I ran and edited my code, below I add a Window and a Frame to a subclass object of Frame. The strange thing is the second add gives the same
error message: why a Frame is considered a Window here? Sure, Frame is a subclass of Window. Any insights?
import java.awt.*;
public class container1 extends Frame
{
public container1() {super();}
public static void main(String[] args)
{
container1 cn=new container1();
Frame f=new Frame();
Window wd=new Window(f);

//IllegalArgumentException:
//adding window to a container
cn.add(wd);
//IllegalArgumentException:
//adding window to a container
cn.add(f);
}
}
victor
Originally posted by Rob Ross:
Valentin,
I'm pretty sure you just made a simple typo above, because the answers are c & e, NOT c & d as you wrote.
It's perfectly legal to add a textarea to a container, but it's an error to try to add the container's parent to the container.

[ February 07, 2002: Message edited by: victor gu ]
[ February 07, 2002: Message edited by: victor gu ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this one:

HIH
[ February 07, 2002: Message edited by: Valentin Crettaz ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic