"AdamB" <cruxic@gmail.com> wrote in news:1136666529.039781.110900
@g43g2000cwa.googlegroups.com:
> Why won't this line compile?:
> super(parent instanceof Dialog?(Dialog)parent:(Frame)parent);
[quoted text clipped - 31 lines]
> Any insight you can give would be appreciated.
> - AdamB
A similar question has come up recently. In JDK 1.4 the ternairy
operator won't compile at all because Dialog and Frame are not directly
related types (meaning neither one is a superclass of the other). In JDK
5.0 the ternairy operator will compile correctly because both types are
interpreted as their most specific common superclass - ie Window.
However, as the error message states, there is no JDialog(Window)
constructor, so although the ternairy operator compiles, the constructor
call doesn't.
If you know, as your code suggests, that parent is either a Dialog or a
Frame, then why would you use pass it to the constructor as Window?
Instead just have two overloaded constructors.
public class Hmm extends JDialog
{
public Hmm(Dialog parent)
{
super(parent);
}
public Hmm(Frame parent)
{
super(parent);
}
}

Signature
Beware the False Authority Syndrome
>In my mind this should work because the JDialog constructor has
>overloads for both Dialog and Frame.
Are you sure about this?
> In my mind this should work because the JDialog constructor has
> overloads for both Dialog and Frame. (Both derive from Window.)
That is right, however the compiler will decide which constructor to
invoke at compile time, not at run time. Since it can not find a
constructor matching the most specific common supertype (Window) it
rejects the invokation.
Using two constructors seems to be the way to go.
ricky.clarkson@gmail.com - 13 Jan 2006 01:58 GMT
> Using two constructors seems to be the way to go.
Well, the constructors are already defined in JDialog, so it would be
easier to just use JDialog, rather than extend it.
JDialog dialog=new JDialog();
dialog.setStuff();
dialog.add(stuff);
dialog.setVisible(true);
ricky.clarkson@gmail.com - 13 Jan 2006 01:59 GMT
Er, that should be JDialog dialog=new JDialog(frame);