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 / December 2007

Tip: Looking for answers? Try searching our database.

JInternalFrame and accessing data between windows

Thread view: 
OldPro - 06 Dec 2007 20:08 GMT
How can I have two windows share the same data?  How do I set the
color in one window to be used in another?
RedGrittyBrick - 07 Dec 2007 15:49 GMT
> How can I have two windows share the same data?

a) By using the Model-View-Controller (MVC) pattern.

b) By having them created in the same class and accessing
instance variables.

etcetera.

> How do I set the color in one window to be used in another?

By having something like a JColorChooser in one window and causing the
appropriate method to be invoked using a reference to (some widget in)
the other window. You could use a state change listener for this.
http://java.sun.com/docs/books/tutorial/uiswing/components/colorchooser.html

If each window is defined in it's own class (which I usually do) then
you could provide a setColor method for the second that is used by the
first.

You might use MVC or perhaps Observer patterns.

Your question suggests you are new to Java. In which case I recommend
http://java.sun.com/docs/books/tutorial/uiswing/TOC.html
OldPro - 11 Dec 2007 16:35 GMT
On Dec 7, 9:49 am, RedGrittyBrick <RedGrittyBr...@SpamWeary.foo>
wrote:
> > How can I have two windows share the same data?
>
[quoted text clipped - 18 lines]
>
> Your question suggests you are new to Java. In which case I recommendhttp://java.sun.com/docs/books/tutorial/uiswing/TOC.html

Yes, I am new to Java, although I have been programming for many
years.  I was looking for more of a "push the green button" answer.
Normally, in other languages, when one wants to share data inside a
program, a global variable is used.  Does Java have a global variable
that can be seen by any classes called by the class where the variable
is defined?  Or do I have to create a listener for each and every
variable that needs to be viewable between classes?
RedGrittyBrick - 11 Dec 2007 17:42 GMT
>>> How can I have two windows share the same data?
>>
[quoted text clipped - 3 lines]
> Yes, I am new to Java, although I have been programming for many
> years.  I was looking for more of a "push the green button" answer.

Sure, but I thought you might also appreciate the "dang, if only I'd
been told about the red and blue buttons three months ago" answer.

> Normally, in other languages, when one wants to share data inside a
> program, a global variable is used.  

True, but often global variables are regarded as a very bad thing (tm).
Of course, like many languages, Java also provides you with the means to
shoot yourself in the foot. I'll assume you know all this and are a
skilled Colt 45 gunslinger picking up a loaded Glock 9mm for the first
time (and that I'm out of range).

> Does Java have a global variable
> that can be seen by any classes called by the class where the
> variable is defined?  

One way ^H^H^H^H^H^H^H^H The green button is ...

  class Global {
     public static Bullet myBullet;
  }

  class Gun {
     Gun() {
       myBullet = new Bullet("OldPro");
       new Foot().shoot();
     }
  }

  class Foot {
    private JTextField flesh;
    public void shoot() {
      flesh.setText(Global.myBullet);
    }
  }

  class Bullet extends String {}

I learned that using the green button was usually a bad choice. Maybe
I'm wrong, you can judge for yourself.

> Or do I have to create a listener for each and
> every variable that needs to be viewable between classes?

Definitely not.

I'd probably be passing needed instance variables as arguments to
methods that, er, need them. Often you can bundle up a whole bunch of
variables as instance variables in a single instance of some container
class.

Good luck! :-)

P.S.
You might like to search Google Groups for prior discussion of "Global
Variables" in comp.lang.java.programming.
RedGrittyBrick - 11 Dec 2007 17:47 GMT
>   class Gun {
>      Gun() {
>        myBullet = new Bullet("OldPro");

    Global.myBullet = new Bullet("OldPro");
    ^^^^^^^

>        new Foot().shoot();
>      }
>   }

I suppose I should test stuff before posting.
OldPro - 11 Dec 2007 19:51 GMT
On Dec 11, 11:47 am, RedGrittyBrick <RedGrittyBr...@SpamWeary.foo>
wrote:
> >   class Gun {
> >      Gun() {
[quoted text clipped - 8 lines]
>
> I suppose I should test stuff before posting.

Thank you for your help!


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



©2009 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.