This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
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.
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 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?
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
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.
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?
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?
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.