Communicating between jFrames

I am fully prepared to be humbled as this is my first post. A few days of checking tutorials, this forumn and a few java books has not yet yielded the solution. I'm certain it is in plain sight and that i am probably just not seeing it. but your help would be greatly appreciated:

My background is mostly in Visual Basic 6 with some perl/python dabled here and there.

My Issue is this:

I am writing an application, the main class is a jFrame form called "JavaTestMain.java". The second part is another jFrame form called "JavaTestWindow.java".

~JavaTestMain loads first and displays some information.

~JavaTestWindow loads next after clicking a button.

~I need the JavaTestWindow form to be able to run a function that resides in the open JavaTestMain form. (I called it "DoSomething")

~I have tried many a things and suggestions but just can't quite get it to work. Below you will see a bare trimmed down example of what I am trying to do. Some help or a point in the right direction would be helpfull.

/*

* JavaTestMain.java

*

* Created on June 4, 2007, 1:34 PM

*/

package mypkg;

/**

*

* @author cadman

*/

public class JavaTestMain extends javax.swing.JFrame {

/** Creates new form JavaTestMain */

public JavaTestMain() {

initComponents();

}

[Swing Code that is generated by NetBeans}

(i am leaving this automated code out, but if it is needed let me know)

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

new JavaTestWindow().setVisible(true);

}

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new JavaTestMain().setVisible(true);

}

});

}

public void DoSomething() {

jTextField1.setText("Hello");

}

// Variables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JTextField jTextField1;

// End of variables declaration

}

AND here is the next one:

/*

* JavaTestWindow.java

*

* Created on June 4, 2007, 1:41 PM

*/

package mypkg;

/**

*

* @author cadman

*/

public class JavaTestWindow extends javax.swing.JFrame {

/** Creates new form JavaTestWindow */

public JavaTestWindow() {

initComponents();

}

[Swing Code that is generated by NetBeans}

(i am leaving this automated code out, but if it is needed let me know)

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

// What should i put here?

}

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new JavaTestWindow().setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JButton jButton2;

// End of variables declaration

}

NOTES: jButton1ActionPerformed is what opens the second window

jButton2ActionPerformed is where i would like to run 'DoSomething' from the first window.

AND that is it. thoughts? suggestions?

[3414 byte] By [amadmana] at [2007-11-27 6:23:25]
# 1

please use code tags (see the "code" button above the message text area) around your code, it makes it much easier to read.

Generally, what I do is provide a way to pass the reference to the JFrame in the ctor of the ActionListener and make it a local variable, then from within the actionPerformed method you can call whatever you want using that reference.

If that makes sense

Good Luck

Lee

tsitha at 2007-7-12 17:41:21 > top of Java-index,Java Essentials,New To Java...
# 2

Look into the MVC pattern, it'll make your life a lot easier. Basically, your controller is in charge of showing/hiding guis (along with most other control functions), and your view is in charge of notifying the controller when the user takes an action (such as clicking the button that's supposed to show the second gui). The frame tells the controller the user wants to do action X, then the controller does it's thing by showing the second gui.

hunter9000a at 2007-7-12 17:41:21 > top of Java-index,Java Essentials,New To Java...
# 3

Tsith/Lee:

I believe i understand what you are saying.

basically, in the jButton1 action event that opens the second window, i should pass along with it a reference to the first window? Then i can use that reference in the second window to access the function that i want to run from the first window.

Am i close? lol.

If so, is there any chance you could provide me a quick code snippet example of what your talking about?

Lastly, I noticed that code format issue. I will do it different on future posts. Is there any way to edit the post so that i can fix that? (i looked around but did not see anything)

Thanks!

amadmana at 2007-7-12 17:41:21 > top of Java-index,Java Essentials,New To Java...
# 4

> Look into the MVC pattern, it'll make your life a lot

> easier. Basically, your controller is in charge of

> showing/hiding guis (along with most other control

> functions), and your view is in charge of notifying

> the controller when the user takes an action (such as

> clicking the button that's supposed to show the

> second gui). The frame tells the controller the user

> wants to do action X, then the controller does it's

> thing by showing the second gui.

That's a good idea. The result is more robust code -- one frame is unaware of

the other. They only know about the controller, which mediates between them.

Hippolytea at 2007-7-12 17:41:21 > top of Java-index,Java Essentials,New To Java...
# 5

> > Look into the MVC pattern, it'll make your life a

> lot

> > easier. Basically, your controller is in charge of

> > showing/hiding guis (along with most other control

> > functions), and your view is in charge of

> notifying

> > the controller when the user takes an action (such

> as

> > clicking the button that's supposed to show the

> > second gui). The frame tells the controller the

> user

> > wants to do action X, then the controller does

> it's

> > thing by showing the second gui.

>

> That's a good idea. The result is more robust code --

> one frame is unaware of

> the other. They only know about the controller, which

> mediates between them.

Just to be difficult, I'll throw in the Observer pattern. The frames raise events which are picked up by the controller, and may be relayed to anyone who registers with the controller to get that type of event.

kevjavaa at 2007-7-12 17:41:21 > top of Java-index,Java Essentials,New To Java...
# 6

hunter9000:

I googled that and found the following article:

http://java.sun.com/developer/technicalArticles/javase/mvc/

I believe i understand what is going on there and i agree that following that framework would indeed make this easier in the future.. however, I am still a bit of a newb when it comes to java programming so I am essentially taking the baby steps approach (walk, step, run) so i'm wondering if there is a simpler solution for my problem?

amadmana at 2007-7-12 17:41:21 > top of Java-index,Java Essentials,New To Java...
# 7

> hunter9000:

>

> I googled that and found the following article:

>

> http://java.sun.com/developer/technicalArticles/javase

> /mvc/

>

> I believe i understand what is going on there and i

> agree that following that framework would indeed make

> this easier in the future.. however, I am still a

> bit of a newb when it comes to java programming so I

> am essentially taking the baby steps approach (walk,

> step, run) so i'm wondering if there is a simpler

> solution for my problem?

Well, if your program is just going to have those 2 frames, and there's only a single way to switch between the two of them, then it might end up being easier for now to follow tsith's advice. BUT, the more complex the program gets, and the more possibility there is that it will grow in the future, the easier things will get if you use a good pattern. Basic MVC is good for a few frames and moderate functionality, and the Observer pattern is great if you have much more that a few frames. Here's a bare bones example.

public class Controller {

public Controller() {

// create frame 1, passing a reference to this to it

}

public void openNewWindowButtonClicked() {

// open the new frame2, passing a reference to this to it.

}

}

public class Frame1 {

public Frame1(Controller c) {

// keep reference to c

}

public void actionPerformed() {

// the open new window button is clicked

c.openNewWindowButtonClicked();

}

}

That should give you an idea of what I'm talking about. I wrote a medium-high complexity app using this type of pattern, and it made things a lot easier than it would have been.

hunter9000a at 2007-7-12 17:41:21 > top of Java-index,Java Essentials,New To Java...
# 8

Aye, as hunter9000 said - if it's just a simple 2 frame handful of buttons, nothing depends on anything else UI then just pass a reference to what you need in the appropriate constructor.

But when things start getting complicated use a pattern as has been shown here. I tend to lean on the mediator pattern:

http://www.patterndepot.com/put/8/mediator.pdf

Good Luck

Lee

tsitha at 2007-7-12 17:41:21 > top of Java-index,Java Essentials,New To Java...
# 9

tsith and hunter9000

Thank you very much, i understand your code snippet and applied it to what i am trying to do and it worked perfectly. For the simple task that i was doing (no more than two forms in this project and its not very complex) that method worked great. However, i do understand what everyone here is saying that it is good to lay a proper foundation so that when things DO get more complex I know the right way to implement a good functional structure.

Thanks again!

:o)

amadmana at 2007-7-12 17:41:21 > top of Java-index,Java Essentials,New To Java...