Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

General Application Help

 
Robert Upshall
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Robert Upshall
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very helpfull code example - thanks.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since it is a singleton all classes (running in the same JVM) can access data in it without passing the reference around.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic