• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

  •  
    Sean Keane
    Ranch Hand
    Posts: 590
    Eclipse IDE Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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?
     
    Bartender
    Posts: 11497
    19
    Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
     
    Darryl Burke
    Bartender
    Posts: 5167
    11
    Netbeans IDE Opera Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Maneesh Godbole wrote:
    Darryl recommended modal not model.



    I plead guilty to editing after the fact.
     
    Sean Keane
    Ranch Hand
    Posts: 590
    Eclipse IDE Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Bartender
    Posts: 11497
    19
    Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I think CardLayout will fit the bill for your requirement.
     
    Sean Keane
    Ranch Hand
    Posts: 590
    Eclipse IDE Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Bartender
    Posts: 11497
    19
    Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Check out the API docs for the frames pack() method (actually inherited from one of it's ancestors)
     
    Sean Keane
    Ranch Hand
    Posts: 590
    Eclipse IDE Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Bartender
    Posts: 11497
    19
    Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 590
    Eclipse IDE Chrome Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Bartender
    Posts: 1104
    10
    Netbeans IDE Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Sean Keane
    Ranch Hand
    Posts: 590
    Eclipse IDE Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 1104
    10
    Netbeans IDE Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 590
    Eclipse IDE Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 1104
    10
    Netbeans IDE Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    If you look closely at this tiny ad, you will see five bicycles and a naked woman:
    New web page for Paul's Rocket Mass Heaters movies
    https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
    reply
      Bookmark Topic Watch Topic
    • New Topic