Results 1 to 4 of 4
- 03-05-2011, 09:09 AM #1Member
- Join Date
- Mar 2011
- Rep Power
Sharing Variables Among Multi-JFrame Environment?
I'm pretty new to Java and have been racking my brains these past few days trying to work out how to achieve this.
I'm am creating an application that stores information about users, to open the application you are first presented with a login screen (username and password). Upon correct entry of user and pass, you are taken to another window where information about the user can be viewed/edited. There is a button on the window that will open another window where yet more information can be viewed/edited. I am declaring each window as a new class and in a new .java source file for ease of maintainence etc. My question is, where should I store this arraylist of users so that all windows can access it knowing that each window is its own class? - Is there a standard way to do this for example?
My initial idea is to declare a runner class which initially opens the user and password window, but I've noticed that when a window opens, the runner class (instigating class) doesn't stop and wait until that window closes, nor have I seen how to send a message back to the runner class. In an ideal world I would have something like this:
Open User/Password windowSend message back to runner that user password correctOpen second window from runner
But how would I pass a message back from the user/password window that there has been a successful login, how also do I make the runner wait for that window to close to continue execution.
I'm sorry that at this stage this problem is rather theoretical and I can't show any specific code. I'd be very grateful of any help!
The user information would likely be an object of a class and this object would hold all the pertinent information. I can think of two ways to have your Runner object wait for the User/Password window to finish processing:
- Have the user/password window be presented in a modal dialog such as a JOptionPane or a modal JDialog, or
- Use an observer pattern set up to listen for when the user is done using the user/password window.
There are likely other ways as well.
- 03-07-2011, 05:12 PM #3Member
- Join Date
- Mar 2011
- Rep Power
Thanks for the advice. I've looked into the idea of using the observer pattern but it appears that there are no easy ways of achieving this as my class extends from JFrame and it would also have to extend from Observable - Java doesn't support multiple inheritance.
It appears that the dialog idea would work but I can't seem to find any examples of how to inherit from this etc. Could you point me in the right direction?
There's no need to extend Observable as you could use the design pattern without using the class. For instance a Window listener can help you observe when one window closes. But regardless, for tutorials on how to use JDialog, check out the Oracle Swing tutorials and the JDialog API. Their use is similar to that of a JFrame.
- By iminhell in forum New To JavaReplies: 6Last Post: 10-31-2010, 11:00 PM
- By elizabeth in forum Advanced JavaReplies: 4Last Post: 08-02-2007, 04:53 AM
- By dcojocaru in forum New To JavaReplies: 2Last Post: 07-22-2007, 12:01 PM