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 / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

GUI / Class instantiation problem...

Thread view: 
Brandon Walters - 10 Mar 2004 03:37 GMT
This is not a homework problem!!

I have two classes, ClassA and ClassB.

ClassA contains main() and handles all the Swing code (sets up the frame and
text boxes etc)
   ClassA contains public print methods that I created to print things to
the GUI

ClassB contains some general methods that I need for my application.

In order for ClassB to use methods from ClassA I had to instantiate ClassA
inside of ClassB.

Everything works but the "problem" is when I call methods from the
instantiated version of ClassA I never see any of output on the GUI / frame
because its being done on a different instance of ClassA.

How do you handle multiple classes in GUI project when you need to
instantiate the "main" class?  Making the print methods static is not an
option.

I hope everyone can understand what I mean.  There are no compile errors or
anything, just the output is happening on a different form.

Thanks,

Brandon
Josef Garvi - 10 Mar 2004 07:07 GMT
> In order for ClassB to use methods from ClassA I had to instantiate ClassA
> inside of ClassB.
>
> Everything works but the "problem" is when I call methods from the
> instantiated version of ClassA I never see any of output on the GUI / frame
> because its being done on a different instance of ClassA.

You'll need to make ClassA a singleton.

First, hide the constructor of ClassA by making it private.
Then create a static method called something like "getInstance()" and write:

private static ClassA classAInstance = null;

public static ClassA getInstance() {
    if (classAInstance == null)
        classAInstance = new ClassA();
    return classAInstance;
}

That way, you'll always be working with the same class instance.

Signature

Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty

Brandon Walters - 10 Mar 2004 20:55 GMT
I have done everything you said but I am still getting problems.  When I
called ClassA print methods from ClassB the calls are working but I can't
see the effects because they are happening in the wrong instance I am
guessing.

When the program is first run there is a "main" instance of ClassA.  Somehow
I need to be drawing on that instance.

I still am unsure of how to just have one instance of ClassA....

Thanks,
Brandon

> > In order for ClassB to use methods from ClassA I had to instantiate ClassA
> > inside of ClassB.
[quoted text clipped - 4 lines]
>
> You'll need to make ClassA a singleton.


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.