Results 1 to 4 of 4
  1. #1
    rmerry is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Sharing Variables Among Multi-JFrame Environment?

    Hi Guys,

    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 window
    Send message back to runner that user password correct
    Open 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!

    Regards,
    Richard

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    rmerry is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    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?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

Similar Threads

  1. Sharing information between tabbed jframe?
    By iminhell in forum New To Java
    Replies: 6
    Last Post: 10-31-2010, 10:00 PM
  2. how to create environment variables
    By elizabeth in forum Advanced Java
    Replies: 4
    Last Post: 08-02-2007, 04:53 AM
  3. Replies: 2
    Last Post: 07-22-2007, 12:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •