my dog learned polymorphism
The moose likes Beginning Java and the fly likes Passing values between windows/frames Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing values between windows/frames" Watch "Passing values between windows/frames" New topic

Passing values between windows/frames

Jeffrey Idea

Joined: Sep 16, 2003
Posts: 6
Hello. I've created an application with two windows/frames. The first window displays the second window with a button click. The second window has a button that changes the value of a variable, lets say it's an int variable called x. What I want to do is to be able to display the value of x in the first window.
How do I do that? I've instantiated the second window class in the first window class and then used a public method called getX( ) declared in the second window which is called using the instantiated object in the first window class. However, i always just get a 0 instead of the value that the second window changed x into.
Help please.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Hi Jeffrey,
Welcome to JavaRanch!
There are no special problems associated with sharing data across separate (J)Frames, other than possible the coordination/timing problem of propagating changes from one window to another.
You say you're calling getX() from code in your first window, but I'm not sure how you're doing this -- what code are you calling it from? The place to deal with this would be the event handler for the second window's button -- i.e.,

Hope this helps.

[Jess in Action][AskingGoodQuestions]

Joined: Aug 16, 2003
Posts: 13
i not sure but did u make an object of that windows and call its field in it
Windows1 w1 = new Window1();
int count=0;

Windows2 w2 = new Window1();
---> Window1 w1 = new Window1();
---> w1.count;

no GOD except ALLAH...
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

"Zero Degree",
You have been asked several times to change your display name to comply with the naming policy.
This is your last warning before your account is deleted.
You can read the naming policy here
Your display name must have two words: your first name, a space, then your last name. Fictituious names are not allowed
You can change your display name here
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The listener approach is where you want to go. I'd change the direction, tho, so Window1 listens for changes made in Window2. Then we could have any number of listeners that want to know about the change.
Take this a step further and you'll wind up with Model View Ccontroller. Say we have the two windows, a controller, and an object that holds the data. Window2 tells the controller the user wants to change X to 2. The controller sets the value on the model. The model publishes a message that says "X just changed". Window1 listens for that message and updates itself. Any number of windows could listen at the same time. A google on "Java MVC" will get you millions of resources for more info.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jeffrey Idea

Joined: Sep 16, 2003
Posts: 6
Im stil not sure how to implement ur suggestion. Wat I hav now is actually 3 classes. The 1st class is simply a Frame that closes. The second one is a Frame that closes but with a button that changes the value of its int variable x and also contains the public method "getX()" which supposedly returns the new value of x to its caller. The 3rd class merely contains the main method and instantiates both the 1st and second classes thus:
class MainClass implements ActionListener {
static Button bshow = new Button("Show Window");
static Button getEks = new Button("Get X");

public static void main (String args []) {
FrameUClose win1 = new FrameUClose("This window closes.");
win1.setLayout(new FlowLayout());
win1.add(bshow); win1.add(getEks);
bshow.addActionListener(new RealWindow());
getEks.addActionListener(new RealWindow());

public void actionPerformed(ActionEvent e) {
ButtonFrame win2 = new ButtonFrame();

if (e.getSource() == bshow) {
win2.setLayout(new FlowLayout());
else if (e.getSource() == getEks) {
System.out.println("x = " + win2.getX());
You see above how I called the method getX() of the win2 object to try to display the value of x. This is obviously wrong. How can I display the new value of x correctly?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
As usual, i went way overboard on an answer. :roll: You've set action listeners on your two buttons to "new RealWindow()". I'm not sure what that is, but since you put the listen methods right there in MainClass I'd bet you really need the listeners to be an instance of MainClass. Try some restructuring:
1) In main() just do this

then move all the code that is in main now into a new runTest method. This makes all the code run in a instance of MainClass instead of in the static main method.
2) Make the button variables member variables, defined outside any method. Just move the lines you have now to right after the "class" definition. Oh, and move the reference to Win2 to a member variable, too. That will give your actionPerformed method access to use the variables.
3) Change to addActionListener(this). Now you're registering this instance of MainClass to be the listener.
See if that gets you any closer.
Jeffrey Idea

Joined: Sep 16, 2003
Posts: 6
THANKS! That did the trick. I think the main issue was the wrong placement of the declaration of win2. By making it a member variable, it was now accessible to the whole MainClass.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Swing / JFC / AWT forum...
[ September 18, 2003: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Passing values between windows/frames
It's not a secret anymore!