Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How to fire off an event that closes another JFrame.

  1. #1
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default How to fire off an event that closes another JFrame.

    I've been noodling with a database program in my spare time. Right now it has 2 different JFrames, 1 to do search queries and another to do inserts. I'm thinking of adding a third menu, a "choice" frame that opens up one of those based on the user's choice. Then have buttons on the search and insertion frames to return to the choice frame.

    So opening the new frames at the click of a button should be easy enough--just create new objects. Detecting when the existing window closes shouldn't be too hard either if I just add some Window Listeners. But how do I close the current JFrame? So suppose you're on the "choice" frame:

    1) Click the "Insert Records" button.
    2) Insertion window opens.
    3) Choice window closes.

    Step 3 is the part where I get stuck.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to fire off an event that closes another JFrame.

    What happened to the idea of reusing objects? Why do you have two JFrames to begin with, is it necessary?

    If it is not necessary, just change the contents of a single frame to suit the current state of the application.

    Assuming it is necessary, will the closed frame be reopened? If so just hide it and show it again later.
    In either case, do not make frame B close/hide frame A directly, include a method in the frame that will cause it to close/hide itself.(See the API)

    Going to need more details if you need more specific assistance

  3. #3
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: How to fire off an event that closes another JFrame.

    What happened to the idea of reusing objects? Why do you have two JFrames to begin with, is it necessary?
    It seemed like the natural choice. The two frames do completely different things, look totally different, are wildly different sizes, and belong to two different classes defined in 2 separate .java files. Also one is a tabbed frame and one is a simpler standard frame. It didn't seem practical to put those both on one JFrame.

    Assuming it is necessary, will the closed frame be reopened? If so just hide it and show it again later.
    Hiding... hadn't thought of that. Thanks.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to fire off an event that closes another JFrame.

    Quote Originally Posted by mstabosz View Post
    The two frames do completely different things, look totally different, are wildly different sizes, and belong to two different classes defined in 2 separate .java files. Also one is a tabbed frame and one is a simpler standard frame. It didn't seem practical to put those both on one JFrame.
    ^^ All completely irrelevant, and should not be considered when deciding to use one or two frames.
    If the user only needs one frame at a time, reuse the frame. If the user will use both frames at the same time, then by all means use two frames, or design the needed components to fit in one

    Think about it from the user's perspective, what will be the best experience for the user

  5. #5
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: How to fire off an event that closes another JFrame.

    Quote Originally Posted by jps View Post
    ^^ All completely irrelevant, and should not be considered when deciding to use one or two frames.
    If the user only needs one frame at a time, reuse the frame. If the user will use both frames at the same time, then by all means use two frames, or design the needed components to fit in one

    Think about it from the user's perspective, what will be the best experience for the user
    I don't understand what you're getting at. What would be the advantage of trying to use just one frame? There are two approaches

    1) Use one frame and change it based on which part of the program (query window or insertion window) the user is accessing.
    2) Use 2 frames, one for the query window and one for the insertion window.

    Either way, the user only sees one window (either query or insertion) at any given time. I'd have all the same coding to make both sets of frame images. There wouldn't be any less code, or any different look to the user.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to fire off an event that closes another JFrame.

    I was not trying to talk you into using just one frame, I was trying to stress that it can go either way, but not seeing more details, I can not suggest which way to go.
    Using two frames is perfectly fine if you, (the author of the code), has determined it is the way to go.

    In the end the difference is using two frames and hiding one is keeping two frames in memory instead of one.
    Don't let me talk you out of your decision, but if I was going to pick, I would rather have just one frame that didn't change size, shape, and possibly position during use, and provided all of the functionality I was going to use, but that is just my opinion based on not knowing any of the details

Similar Threads

  1. need help on jframe buttonclicked event
    By Eatiem0on in forum AWT / Java Swing
    Replies: 2
    Last Post: August 5th, 2013, 10:57 AM
  2. How to save an updated JList to a text file after the GUI closes?
    By arob317 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: February 22nd, 2013, 03:56 PM
  3. JFrame in event despatch thread
    By DanielJamesCollier in forum Java Theory & Questions
    Replies: 1
    Last Post: April 25th, 2012, 11:56 AM