using JFrame before constructor invocation

M

Martijn Mulder

Can I use the 'this'-pointer to the not-yet constructed JFrame?

class MyClass extends javax.swing.JFrame
{
javax.swing.JDialog jdialog=new javax.swing.JDialog(this,"MyDialog");
MyClass(){super("MyFrame");}
}
 
T

Thomas Hawtin

Martijn said:
Can I use the 'this'-pointer to the not-yet constructed JFrame?

class MyClass extends javax.swing.JFrame
{
javax.swing.JDialog jdialog=new javax.swing.JDialog(this,"MyDialog");
MyClass(){super("MyFrame");}
}

The initialisers run just after super returns, so as far as JFrame the
object is constructed.

OTOH, there is rarely any need to extend JFrame at all.

Tom Hawtin
 
M

Martijn Mulder

The initialisers run just after super returns, so as far as JFrame the
object is constructed.


I am afraid that is not so. The output of this tiny program indicates that
the JDialog is constructed before the constructor of JFrame is called:

class MyDialog extends javax.swing.JDialog
{
MyDialog(javax.swing.JFrame a){System.out.println("MyDialog.constructor");}
}

class MyFrame extends javax.swing.JFrame
{
MyDialog mydialog=new MyDialog(this);
MyFrame(){super();System.out.println("MyFrame.constructor");}
public static void main(String[]a){new MyFrame();}
}

//output:
//MyDialog.constructor
//MyFrame.constructor
 
M

Martijn Mulder

Can I use the 'this'-pointer to the not-yet constructed JFrame?
I am afraid that is not so. The output of this tiny program indicates
that
the JDialog is constructed before the constructor of JFrame is called:

No it doesn't.
class MyDialog extends javax.swing.JDialog
{
MyDialog(javax.swing.JFrame
a){System.out.println("MyDialog.constructor");}
}

class MyFrame extends javax.swing.JFrame
{
MyDialog mydialog=new MyDialog(this);
MyFrame(){super();System.out.println("MyFrame.constructor");}
public static void main(String[]a){new MyFrame();}
}

//output:
//MyDialog.constructor
//MyFrame.constructor

As I say, the initialiser for jdialog runs *immediately* after super()
returns. Your code prints "MyFrame.constructor" after super() returns, and
after the initialisers have run.

class MyDialog extends javax.swing.JDialog {
MyDialog(javax.swing.JFrame frame) {
System.out.println(
"MyDialog.constructor, frame.title: "+frame.getTitle()
);
}
}

class MyFrame extends javax.swing.JFrame {
private MyDialog mydialog = new MyDialog(this);
MyFrame() {
super(print("--- title ---"));
System.out.println("MyFrame.constructor, after super");
}
private static String print(String msg) {
System.out.println("Print: "+msg);
return msg;
}
public static void main(String[] args){
new MyFrame();
}
}

$ java MyFrame
Print: --- title ---
MyDialog.constructor, frame.title: --- title ---
MyFrame.constructor, after super

The title property is set in the JFrame constructor. The MyDialog
constructor reads that property, so must have printed the message after
the JFrame constructor ran.



Very convincing example. Thanks a lot!
 
T

Thomas Hawtin

Martijn said:
I am afraid that is not so. The output of this tiny program indicates that
the JDialog is constructed before the constructor of JFrame is called:

No it doesn't.
class MyDialog extends javax.swing.JDialog
{
MyDialog(javax.swing.JFrame a){System.out.println("MyDialog.constructor");}
}

class MyFrame extends javax.swing.JFrame
{
MyDialog mydialog=new MyDialog(this);
MyFrame(){super();System.out.println("MyFrame.constructor");}
public static void main(String[]a){new MyFrame();}
}

//output:
//MyDialog.constructor
//MyFrame.constructor

As I say, the initialiser for jdialog runs *immediately* after super()
returns. Your code prints "MyFrame.constructor" after super() returns,
and after the initialisers have run.

class MyDialog extends javax.swing.JDialog {
MyDialog(javax.swing.JFrame frame) {
System.out.println(
"MyDialog.constructor, frame.title: "+frame.getTitle()
);
}
}

class MyFrame extends javax.swing.JFrame {
private MyDialog mydialog = new MyDialog(this);
MyFrame() {
super(print("--- title ---"));
System.out.println("MyFrame.constructor, after super");
}
private static String print(String msg) {
System.out.println("Print: "+msg);
return msg;
}
public static void main(String[] args){
new MyFrame();
}
}

$ java MyFrame
Print: --- title ---
MyDialog.constructor, frame.title: --- title ---
MyFrame.constructor, after super

The title property is set in the JFrame constructor. The MyDialog
constructor reads that property, so must have printed the message after
the JFrame constructor ran.

Tom Hawtin
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top