Java conflicts to itself

I just tried to add a JFrame to a JPanel. Which is possible if you go to rules. Because JPanel's add method takes a Component as a parameter and JFrame is a component. But it throws exception saysadding window to a panel. I know it is foolish step but is it not a confliction to java class heriearchy and methods?

Regards

[343 byte] By [Christia] at [2007-11-27 11:10:06]
# 1

not at all. If the add method has been implemented to accept only specific types of components (or to reject specific types) there's nothing wrong with that.

jwentinga at 2007-7-29 13:39:34 > top of Java-index,Java Essentials,New To Java...
# 2

sigh,... If I had a dollar for every newbie who "found" a java bug...

petes1234a at 2007-7-29 13:39:34 > top of Java-index,Java Essentials,New To Java...
# 3

Don't you think someone would have noticed this "bug" before now?

georgemca at 2007-7-29 13:39:34 > top of Java-index,Java Essentials,New To Java...
# 4

hmm, adding window to panel is a bug? >.<"

I would never do like that though I'm a newbie too T_T

Jensona at 2007-7-29 13:39:34 > top of Java-index,Java Essentials,New To Java...
# 5

They could have written the compiler to detect these special cases, and throw a wobbler. They could have. They could have written it to catch a lot of stupid coding mistakes, but why should it? It's a compiler, not a babysitter. What we've got here is a quirk of the JComponent hierarchy, and the fact that all JComponents are awt Containers - nothing more. To code around all of these things would not only bloat the compiler, it would introduce hundreds of instances of special case code, which is highly undesirable, and a haven for bugs. For what? To stop noobs doing something inadvisable, and nothing more

georgemca at 2007-7-29 13:39:34 > top of Java-index,Java Essentials,New To Java...
# 6

and what if a 3rd party API displayed similar behaviour? How would the compiler catch incorrect use of that?

And why should it detect incorrect use of the standard libraries (which are no different from 3rd party libraries as far as the compiler is concerned) if it shouldn't detect it everywhere?

jwentinga at 2007-7-29 13:39:34 > top of Java-index,Java Essentials,New To Java...
# 7

> and what if a 3rd party API displayed similar

> behaviour? How would the compiler catch incorrect use

> of that?

If everyone were using this super-duper nanny compiler from h3ll, it would catch it when the 3rd party compiled their code :-)

*sigh* it's so hard to explain to noobs why "java" allows us to do stupid things. Thank god this isn't an ASM forum. Can you imagine?

georgemca at 2007-7-29 13:39:34 > top of Java-index,Java Essentials,New To Java...
# 8

> *sigh* it's so hard to explain to noobs why "java"

> allows us to do stupid things. Thank god this isn't

> an ASM forum. Can you imagine?

yah! ASM still remember that language. ^_^

/*

mov ax, bx

cmp ax, bx

jnz do_something

*/

Message was edited by:

Yannix

Yannixa at 2007-7-29 13:39:34 > top of Java-index,Java Essentials,New To Java...
# 9

> > *sigh* it's so hard to explain to noobs why "java"

> > allows us to do stupid things. Thank god this

> isn't

> > an ASM forum. Can you imagine?

>

> yah! ASM still remember that language. ^_^

>

> /*

> mov ax, bx

> cmp ax, bx

> jnz do_something

> */

>

> Message was edited by:

> Yannix

heh heh so you'll remember how easy it was to shoot yourself in the foot. From behind. Around corners. Imagine if these guys who think javac should hold your hand and not let you do anything silly got hold of an assembler?! Yikes!

georgemca at 2007-7-29 13:39:34 > top of Java-index,Java Essentials,New To Java...
# 10

then again, they'd have to recover their machine from a corrupt operating system after every attempt to run anything, so the rate of posts per punter would be a lot lower :)

jwentinga at 2007-7-29 13:39:34 > top of Java-index,Java Essentials,New To Java...
# 11

> They could have written the compiler to detect

> these special cases, and throw a wobbler. They

> could have. They could have written it

> to catch a lot of stupid coding mistakes, but why

> should it? It's a compiler, not a babysitter. What

> we've got here is a quirk of the JComponent

> hierarchy, and the fact that all JComponents are awt

> Containers - nothing more. To code around all of

> these things would not only bloat the compiler, it

> would introduce hundreds of instances of special case

> code, which is highly undesirable, and a haven for

> bugs. For what? To stop noobs doing something

> inadvisable, and nothing more

Rubbish. The Java compiler is the closest thing to a babysitter that you can get programming language wise. =P

Edit: Ok maybe not the closest, but still; I am not sure that that statement is 100% accurate. There ARE instances where Java will go out of its way to babysit you.

Message was edited by:

jGardner

jGardnera at 2007-7-29 13:39:34 > top of Java-index,Java Essentials,New To Java...
# 12

> I just tried to add a JFrame to a JPanel. Which is

> possible if you go to rules.

What rules?

> Because JPanel's add

> method takes a Component as a parameter and JFrame is

> a component. But it throws exception says adding

> window to a panel.

If you read the API this behavior is clearly defined.

From Container.add...This is a convenience method for addImpl(java.awt.Component, java.lang.Object, int)....

From Container.addImpl...Throws:...

IllegalArgumentException - if adding a window to a container ...

> I know it is foolish step but

Yes it is.

> is it not a confliction to java class heriearchy and

> methods?

No.

> Regards

Ditto.

dwga at 2007-7-29 13:39:34 > top of Java-index,Java Essentials,New To Java...