• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing values between windows/frames

 
Jeffrey Idea
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Jeff
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
F A H A D
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
thanks,
Dave
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeffrey Idea
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,
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);
win1.setSize(400,400);
win1.setVisible(true);
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());
win2.setSize(200,200);
win2.setVisible(true);
}
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?
Jeff
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,
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.
Jeff
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Swing / JFC / AWT forum...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic