This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
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.,
"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
Joined: Jan 29, 2003
Posts: 8791
posted
0
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
Greenhorn
Joined: Sep 16, 2003
Posts: 6
posted
0
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
Joined: Jan 29, 2003
Posts: 8791
posted
0
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
Joined: Sep 16, 2003
Posts: 6
posted
0
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