Results 1 to 4 of 4
  1. #1
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Question Sharing Data Members between two JFrames

    I have two classes that extend JFrame, call them frame1 and frame2. Frame1 is basically the main window while frame2 is an "options" window.

    Frame1 has a data member called "mystring". It also has a button called "optionsButton" which loads the options window.

    In the options window I have yet another button that is called "resetStringButton". When I click on this button, it resets the string "mystring" in frame1, to "".

    So basically I want both frames to share mystring. How can I do this? I tried having yet another class that contains the data, and loads both frames. However, I still find it difficult to "share" these members between all these different classes.

    Help would be much appreciated.

  2. #2
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default

    I think I just solved it, although I'm not sure if this is a safe solution or what not. Let me know if this is the correct solution:


    In frame1, I define a new frame1 and set it equal to "this." However, I also declare it as final. Example:

    final frame1 thisframe = this;

    Then... for the action event for the options button, I do this:
    public void actionPerformed(ActionEvent e){
    frame2 optionsframe = new frame2(thisframe);

    }

    .... NOW, in frame2, I wrote a constructor that takes a frame1 as a parameter, AND I also add a frame1 class variable to frame2, called parentframe.

    So now in the ActionListener for the resetStringButton, I have

    public void actionPerformed(ActionEvent e){
    parentframe.mystring = "";
    }


    That's it. It seems to work. I don't know if it's safe or not. Let me know if you see problems with this approach.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    1) Avoid using two JFrames as the second window is behaving as a dialog and thus should be a dialog such as a JDialog. Since you can add to a JDialog's contentPane just as you do with a JFrame, adding JPanels to it is no different. Also, it seems as if your dialog should be a modal one -- one that prevents interaction with the parent window until the dialog is dealt with and so your JDialog should be constructed to be modal.

    2) It's not uncommon to pass a reference from one object into another via a constructor parameter just as you are doing.

    3) You shouldn't have one object directly fiddle with another's fields. Instead give your main window a public method, say public void clearMyString(), and in this method set myString to "". Then call this method from the object passed into the second window/object.

    Luck!

  4. #4
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default

    Thanks for the info I changed it to a JDialog and seems to be working well. I will implement the setter method as well. :)

Similar Threads

  1. Sharing data between instances of an application
    By mordjah in forum New To Java
    Replies: 1
    Last Post: 09-22-2010, 08:29 PM
  2. Help: passing data from multiple JFrames
    By newicons in forum New To Java
    Replies: 5
    Last Post: 06-26-2010, 03:55 PM
  3. private data members?
    By blueduiker in forum New To Java
    Replies: 10
    Last Post: 01-19-2010, 12:13 PM
  4. sharing of data between sites
    By jyovasinedu in forum Advanced Java
    Replies: 1
    Last Post: 11-02-2007, 09:56 AM
  5. 2 threads sharing a data base connection
    By Ed in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 05:41 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •