This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes General Application Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "General Application Help" Watch "General Application Help" New topic
Author

General Application Help

Robert Upshall
Greenhorn

Joined: Aug 23, 2001
Posts: 25
Hi,
I have a question that I think has a very simple answer, forgive my ignorance if it is a dumb question.
I am currently attempting my first set of Swing applications, I am moving to swing from a servlet background.
I have written small swing applications before but I am about to start a larger scale project and I need to know if there is a swing/application way of doing something I found quite useful with servlet programming.
I need to know how can a put something in "memory" and access it in any called class under it. i.e. is their a swing/application equivalent of a session
I want for example to start my app, load a config file, and put the config setting somewhere that can be reached by anything else used in that application.
This might be a stupid question or it might not be doable but I'm looking for a way to pass data between swing class without having to pass them on the constructor of each class.
I hope this makes sense,
Robert
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

There is nothing "SWING" about putting objects into memory. Use a Vector, use a HashTable or Hashmap, use whatever kind of object you want to store your information. You just have to make it public to whatever class you want to access it in.
It is the same as any other JAVA Library.


GenRocket - Experts at Building Test Data
Robert Upshall
Greenhorn

Joined: Aug 23, 2001
Posts: 25
I may not have asked the question properly or I may just not understand the anwser.
I know I can store info in a Vector or a Hashtable, my question was ment to be more how do I access it.
ie.
App1 -(click)-> [Button1}

[Button1] -(triggers)-> new App2()

if in App1 I enter my name, how
do I put in somewhere that I can go
get it in App2
In a servlet I would just put it in the session and then get it again, my question is ment to be where/how do I put it somewhere so I can do a get of sort in App2 to get it.
Robert
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You either pass the data to app2 from app1 directly when you launch it, or through some kind of setData() method that you make up... The way I've usually done this is to fill up a HashMap with data gathered from all the fields of app1 in the action listener of the button in app1 and pass it to a setData(HashMap) method I made in app2 that fills out the fields of app2 with data from the HashMap. Note that both apps must agree on what keys you put data under in the HashMap.

If you need some global data that all apps may need to access, I would make a singleton class that holds a HashMap or Properties object and I set up constant String values for the keys to the data. Then any class that needs global data can just call this class...


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You can do a singleton "registry" class like this....



Then just have a "constant keys" class to make sure all classes use the same keys...



Of course, you'll probably want to name your keys something more meaningful...

This way, you can call -


And -




You'll need to do the normal HashMap stuff... make sure the value you are getting back is not null and casting it to the class that you need...

If you need to know when keys get updated, you'll need to look into the PropertyListener event model...
Robert Upshall
Greenhorn

Joined: Aug 23, 2001
Posts: 25
Nate,
Thanks so much for the last post, I have been looking for a method like that since I started playing with application programming. It was exactly what I was looking for.
Thanks,
Robert
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Very helpfull code example - thanks.


Please ignore post, I have no idea what I am talking about.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Now that I look at this code example it looks like java.util.Properties Except that it is a singleton. What benefits do you get from having a singleton version? Thanks.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Since it is a singleton all classes (running in the same JVM) can access data in it without passing the reference around.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: General Application Help