File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Swing GUI Method Access Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing GUI Method Access Question" Watch "Swing GUI Method Access Question" New topic
Author

Swing GUI Method Access Question

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I have been programming SWING GUI's for quite a while now. However, I am looking for the proposed/best method to do the following and I am sure that this has been answered here before, but am unable to find a good answer with the search tool.
In keeping my GUI's very modular I try and create seperate classes for almost everything. So below is the typical set of classes I have on most GUI's:
MainFrame - extends JFrame
MainMenu - extends JMenuBar
MainToolBar - extends JToolBar
InternalFrame1 - extends JInternalFrame
InternalFrame2 - extends JInternalFrame
....
....
Anyway, you get the idea. What I am wondering is what is the best method for the MainMenu and MainToolBar action events to interact/call methods in several different classes without creating a bunch of public static variables?
Also, what is the best method for say JInternalFrame1 to interact with JInternalFrame2? Should I use the singleton pattern on both and have each one request the instance() of that object when needed?
BTW - I have been reading a lot on MVC and Obersable/Observer classes. I don't know how well I am grasping it though. The examples I have seen don't make much since to me yet.
Any comments, suggestions, code, links, examples, etc, are welcome.
Thanks.


GenRocket - Experts at Building Test Data
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Anyway, you get the idea. What I am wondering is what is the best method for the MainMenu and MainToolBar action events to interact/call methods in several different classes without creating a bunch of public static variables?


I'm not really sure what you mean by this... Do you mean that when you choose a Menu Item or a button on the toolbar you want multiple methods in different classes to be called? Make action listeners that call the specified method of the class and add each of them to the menu item. This is assuming that order of method execution doesn't matter, and that subsequent method calls do not depend on the results of prior method calls. If so, you'll need to make listeners that the required classes have to register with.


Also, what is the best method for say JInternalFrame1 to interact with JInternalFrame2? Should I use the singleton pattern on both and have each one request the instance() of that object when needed?


I've seen a lot of people do this, and it works out OK if you only have a few different internal frames... if you are going to have a lot, none of the instances will get garbage collected once they are launched, so you can start wasting memory. Another problem with only using singletons is that, sooner or later, the design gets changed a little bit and singletons aren't applicable anymore. (i.e. you originally design all internal frames as singletons, then decide the user should be able to view multiple equipment screens at the same time. Unfortunately, you've already developed the equiptment screen as a singleton...)

Just a few ideas...


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Thanks for the response Nate.
Basically what I am saying about the menu and tool bar is this:
Class A:
Menu Bar
File
New
Class B
public void createNew() { }
Now, in order for Class A - New to call Class B createNew() I would have to have an instance of Class B in Class A:
ClassB cb = new ClassB()
The other way is to change the createNew() method to be static:
public static void createNew() { }
And then in Class A:
ClassB.createNew(); on the ActionEvent of the New Menu option.
The scenerio is this; I have items and processes in my constructor of Class B that don't need to happen all the time. Really, only the first time this class is instantiated. I don't want to do the singleton pattern on it for the same reasons you stated in your response.
I also am wondering if it is a problem using static methods like I stated above and just calling the method that way?
What is the best practice I guess is what I am saying??
I hope that made some sense.
Thanks.
Bill Liteplo
Ranch Hand

Joined: Oct 16, 2002
Posts: 88
A few suggestions,
I find it useful to separate the actions from the menu items/buttons that invoke them. So you can define a simple menu and fill it up, but create the menu items with actions, and they will be hooked up fine, including enabling/disabling.
We have separate classes for each high-level action (such as Cut/Copy/Paste, etc.) that are non-static, but instantiate a static instance of each within the class, and provide a method to get this static instance. E.g.

Additionally, we have a concept of global selection which allows us to get the thing that is currently selected in the application, regardless of which window it is in, and act on it. The global selection adapter is also static.

If your main frame can keep track of your internal frames, that sounds like a good place to do the management of linking your classes (non-statically). If not, you can have your internal frames manage themselves with a static reference. No harm in that.

It all really depends hom complicated your application is. All the stuff I mentioned before is after years of more local (non-static) management of actions and windows, which was fine for a while.
Hope this doesn't confuse you more.
Bill
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Bill, thanks for the advice/suggestion. I am still confused about one thing though.
In the actionPerformed method of your example I still am missing the point in which we invoke methods that may lie in another class without public static methods or instantiating the class say in the Menu Class that I have created. Or maybe I shouldn't be doing that?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Swing GUI Method Access Question
 
Similar Threads
call to JTextArea.getText() is not getting current text
Connecting components in different files
redirecting to an other page problem
Swing Stuff
Calling methods 'up the chain'