Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

using JFrame before constructor invocation

Thread view: 
Martijn Mulder - 13 Feb 2006 19:19 GMT
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");}
}
Thomas Hawtin - 13 Feb 2006 19:44 GMT
> Can I use the 'this'-pointer to the not-yet constructed JFrame?
>
[quoted text clipped - 3 lines]
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Martijn Mulder - 13 Feb 2006 20:39 GMT
>> Can I use the 'this'-pointer to the not-yet constructed JFrame?
>>
[quoted text clipped - 3 lines]
>> MyClass(){super("MyFrame");}
>> }

> 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
Thomas Hawtin - 13 Feb 2006 21:49 GMT
>>>Can I use the 'this'-pointer to the not-yet constructed JFrame?
>>>
[quoted text clipped - 9 lines]
> 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
> {
[quoted text clipped - 11 lines]
> //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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Martijn Mulder - 13 Feb 2006 21:55 GMT
>>>>Can I use the 'this'-pointer to the not-yet constructed JFrame?
>>>>
[quoted text clipped - 65 lines]
> constructor reads that property, so must have printed the message after
> the JFrame constructor ran.

Very convincing example. Thanks a lot!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.