File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Correct behaviour when launching a new window and getting rid of the first window Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Correct behaviour when launching a new window and getting rid of the first window" Watch "Correct behaviour when launching a new window and getting rid of the first window" New topic
Author

Correct behaviour when launching a new window and getting rid of the first window

Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Hi, I am pretty new to Swing and I was wondering if anyone had any advice on the correct way to handle a scenario that I have.

The scenario is that I have a JFrame that allows the user enter some information and then click on a button. When they click on the button I want to create a new window and get rid of the first window.

At the moment I have handled this by hiding the first window and then creating\displaying the second window.

Should I be disposing of the first window in some way rather than just hiding it?

Also, are my two calls inside the openSecondWindow safe? I think they are because I read somewhere that an acitonPerformed method is always guaranteed to be executed on the event dispatch thread. So there should be no need to put my two calls inside an invokeLater call. Does this should right?



SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4522
    
    5

When they click on the button I want to create a new window and get rid of the first window.


Two better approaches are --
  • use a modal JDialog
  • use a CardLayout


  • luck, db
    There are no new questions, but there may be new answers.
    Sean Keane
    Ranch Hand

    Joined: Nov 03, 2010
    Posts: 581

    Darryl Burke wrote:
    When they click on the button I want to create a new window and get rid of the first window.


    Two better approaches are --
  • use a model JDialog
  • use a CardLayout



  • Thanks Darryl. What is better or what are the benefits of using a JDialog?
    Maneesh Godbole
    Saloon Keeper

    Joined: Jul 26, 2007
    Posts: 9990
        
        7

    Sean Keane wrote:
    Darryl Burke wrote:
    When they click on the button I want to create a new window and get rid of the first window.


    Two better approaches are --
  • use a model JDialog
  • use a CardLayout



  • Thanks Darryl. What is better or what are the benefits of using a JDialog?

    Darryl recommended modal not model.
    A Dialog can be modal. When a modal Dialog is visible, it blocks user input to all other windows in the program
    More info here http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html
    One would typically use dialogs when your application needs some input or confirmation from the user. One important point to remember is that the main UI is already showing, but the operation cannot proceed without the user interaction. e.g. The user might click on some button which deletes some critical data. You want the user to confirm that deleting is what he meant. So you might show a dialog, displaying some text like "Are you absolutely positive you want to delete this data? Once deleted, there is no way to recover it back" and ask for his confirmation. As you can see from the user's perspective, there are two "windows" visible on the screen.

    On the other hand, CardLayout provides you the ability to change the contents of the main window. Think of it as a browser. You open a page, in the browser window. You click on a link and the content changes in the same browser window. Check out the API docs for CardLayout for more info on how its done.


    [How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
    Darryl Burke
    Bartender

    Joined: May 03, 2008
    Posts: 4522
        
        5

    Maneesh Godbole wrote:
    Darryl recommended modal not model.


    I plead guilty to editing after the fact.
    Sean Keane
    Ranch Hand

    Joined: Nov 03, 2010
    Posts: 581

    Maneesh Godbole wrote:One important point to remember is that the main UI is already showing, but the operation cannot proceed without the user interaction.


    This is not the behaviour that I had intended on going with. I don't want the main UI showing unless the information provided is correct.

    I am working on the OCMJD certification. I'm not sure if you are familiar? But it is basically an application for reserving hotel rooms. The application needs the location of a database file in order to start up.

    My approach is that the first thing the user will see will be a window asking them to enter the location of the database. This window will have a connect and a cancel button. If this database location is pointing at a valid database file when the user clicks on the connect button then I will launch the main application window that will display the records.

    I'm not sure if a JDialog would be the best approach here - I don't want to dismiss the window when the user enters incorrect information. I want to give them a warning that the information that they entered is incorrect. I will only dismiss the window if the information is correct (in which case I will launch the main UI) or if the user clicks on the cancel button.
    Maneesh Godbole
    Saloon Keeper

    Joined: Jul 26, 2007
    Posts: 9990
        
        7

    I think CardLayout will fit the bill for your requirement.
    Sean Keane
    Ranch Hand

    Joined: Nov 03, 2010
    Posts: 581

    Maneesh Godbole wrote:I think CardLayout will fit the bill for your requirement.


    Yep, this sounds like a good approach. The one thing I am wondering about is the size of my window. Can you resize the window when displaying different cards?

    I've done some searching on this but not found any solutions. My next steps is to throw together a simple example to try it out. But just wondering if anyone knows here in advance.

    The reason I would want to change the window size is that the first card will contain very little - just two buttons (connect & cancel) and a browse button with a text field for the file location. Whereas the second card will contain a JTable full of records. So if I am to use the same size window for both cards, then the first card will have a lot of empty space.

    Maybe resizing the window when using the CardLayout is a bad approach and I should just live with the extra space in the first card and position the elements in the center of the card?
    Maneesh Godbole
    Saloon Keeper

    Joined: Jul 26, 2007
    Posts: 9990
        
        7

    Check out the API docs for the frames pack() method (actually inherited from one of it's ancestors)
    Sean Keane
    Ranch Hand

    Joined: Nov 03, 2010
    Posts: 581

    The pack method does not seem to have any effect after the window is displayed for the first time.

    I call the pack method when the card is changed each time - but it has no effect.

    I've tried setting the preferred size for each card and that had no effect either. But ideally I don't want to have to set a preferred size. I just want the window to resize to fit the contents.

    So how do I get the window to change size when the card changes?


    Maneesh Godbole
    Saloon Keeper

    Joined: Jul 26, 2007
    Posts: 9990
        
        7

    Ok. It seems since the cards are already added, they are taken into consideration when resizing the frame.
    Instead of card layout, I changed to manually remove and add children. Notice the call to revalidate() as well as repaint().
    It does not follow good coding practices but will do for an SSCCE

    Sean Keane
    Ranch Hand

    Joined: Nov 03, 2010
    Posts: 581

    Thanks for that. But I think I will go with the CardLayout and just live with the fact that the panels must be the same size.

    I'm not sure if it would look good anyhow when the user clicks on the button on the first panel and then the window jumps to a different size when the second panel is displayed.

    I'll just fill up the first window by having some banner at the top, put my browse button and field centered in the middle, then put my navigation buttons at the bottom.
    Ranganathan Kaliyur Mannar
    Bartender

    Joined: Oct 16, 2003
    Posts: 1075
        
      10

    I think your case is similar to the usual situation of asking login credentials in a small window - if login is successful, you show the main application. If not, you show an error message in red. I think JDialog suits the best. I would have the small database selection window as a JDialog. And then launch the main application if connection is successful.


    Ranga.
    SCJP 1.4, OCMJEA/SCEA 5.0.
    Sean Keane
    Ranch Hand

    Joined: Nov 03, 2010
    Posts: 581

    Ranganathan Kaliyur Mannar wrote:I think your case is similar to the usual situation of asking login credentials in a small window - if login is successful, you show the main application. If not, you show an error message in red. I think JDialog suits the best. I would have the small database selection window as a JDialog. And then launch the main application if connection is successful.


    So what happens when the user enters incorrect information and clicks on the "OK" button? The JDialog disappears?

    What I would want is that the JDialog only disappears under two scenarios:

    1) If the information provided is correct when the user clicks on the "OK" button.
    2) The user clicks on the "Cancel" button.

    But shouldn't a JDialog disappear when you click on the "OK" button?
    Ranganathan Kaliyur Mannar
    Bartender

    Joined: Oct 16, 2003
    Posts: 1075
        
      10

    Sean Keane wrote:So what happens when the user enters incorrect information and clicks on the "OK" button? The JDialog disappears?

    No. You display the error message in a JLabel with a red font on the dialog itself. So, in the ActionListener of the ok button, you would first do a validation and then decide what to do.
    Sean Keane
    Ranch Hand

    Joined: Nov 03, 2010
    Posts: 581

    So my use case here is that I want a window to appear where the user will enter some information. If the information entered is correct and they click on the "OK" button, then the main application window will be launched.

    Based on this use case I am confused as to why it is suggested to use JDialog over JFrame?

    From a technical perspective is there any reason why one would use JDialog instead of JFrame in this scenario? It's not like I am displaying a dialog when my main application is visible and want to have it as a modal dialog.

    Is it simply people are suggesting JDialog because that is generally used to get user input and that there is no technical reason to choose JDialog over JFrame in my scenario?
    Ranganathan Kaliyur Mannar
    Bartender

    Joined: Oct 16, 2003
    Posts: 1075
        
      10

    This is a good question. I think it is more by convention that I suggested JDialog.
    However, a dialog can still be useful in some cases. For example, you mentioned about the initial window where user will select a database from a list - I would suppose this would be displayed in a combo box with just ok and cancel buttons. In this case, you can use the convenient as against building your own frame.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Correct behaviour when launching a new window and getting rid of the first window
     
    Similar Threads
    How to build a frame ?
    Blank Frame or Panel
    DefaultTableModel help
    Bad Background Refresh
    counter is not resetting