Results 1 to 4 of 4
- 10-15-2010, 12:24 AM #1
Member
- Join Date
- Jul 2010
- Location
- Lima, Peru
- Posts
- 47
- Rep Power
- 0
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.
- 10-15-2010, 12:34 AM #2
Member
- Join Date
- Jul 2010
- Location
- Lima, Peru
- Posts
- 47
- Rep Power
- 0
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.
-
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!
- 10-17-2010, 02:51 AM #4
Member
- Join Date
- Jul 2010
- Location
- Lima, Peru
- Posts
- 47
- Rep Power
- 0
Similar Threads
-
Sharing data between instances of an application
By mordjah in forum New To JavaReplies: 1Last Post: 09-22-2010, 07:29 PM -
Help: passing data from multiple JFrames
By newicons in forum New To JavaReplies: 5Last Post: 06-26-2010, 02:55 PM -
private data members?
By blueduiker in forum New To JavaReplies: 10Last Post: 01-19-2010, 11:13 AM -
sharing of data between sites
By jyovasinedu in forum Advanced JavaReplies: 1Last Post: 11-02-2007, 08:56 AM -
2 threads sharing a data base connection
By Ed in forum Advanced JavaReplies: 2Last Post: 07-04-2007, 04:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks