Java experts: Why won't this line compile?

A

AdamB

Why won't this line compile?:
super(parent instanceof Dialog?(Dialog)parent:(Frame)parent);

The compiler complains with this:

C:\tmp>javac Hmm.java
Hmm.java:10: cannot find symbol
symbol : constructor JDialog(java.awt.Window)
location: class javax.swing.JDialog
super(parent instanceof
Dialog?(Dialog)parent:(Frame)parent);
^


Here's the line in context:

Hmm.java:

import java.awt.Window;
import java.awt.Dialog;
import java.awt.Frame;
import javax.swing.JDialog;

public class Hmm extends JDialog
{
public Hmm(Window parent)
{
super(parent instanceof Dialog?(Dialog)parent:(Frame)parent);
}
}

In my mind this should work because the JDialog constructor has
overloads for both Dialog and Frame. (Both derive from Window.)

Any insight you can give would be appreciated.
- AdamB
 
Z

zero

Why won't this line compile?:
super(parent instanceof Dialog?(Dialog)parent:(Frame)parent);

The compiler complains with this:

C:\tmp>javac Hmm.java
Hmm.java:10: cannot find symbol
symbol : constructor JDialog(java.awt.Window)
location: class javax.swing.JDialog
super(parent instanceof
Dialog?(Dialog)parent:(Frame)parent);
^


Here's the line in context:

Hmm.java:

import java.awt.Window;
import java.awt.Dialog;
import java.awt.Frame;
import javax.swing.JDialog;

public class Hmm extends JDialog
{
public Hmm(Window parent)
{
super(parent instanceof Dialog?(Dialog)parent:(Frame)parent);
}
}

In my mind this should work because the JDialog constructor has
overloads for both Dialog and Frame. (Both derive from Window.)

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);
}
}
 
S

Stefan Schulz

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.
 
R

ricky.clarkson

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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top