Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Closing a dialog in Swing?  RSS feed

 
M Burke
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am calling a dialog from the main frame using show(). Once the user has set her input and hits the OK button, I want the dialog to close. How do I do this? Its a JDialog.

Also, the dialog is appearing in the upper left courner of the screen. I want it to appear in the middle. How do I do this?

 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
invoke hide().
As for centering, get a java.awt.Toolkit instance and invoke getScreenSize. Use getBounds to get the dialog's width and height. Figure out the x and y coordinates to center the dialog (x = half the difference between the screen width and the dialog width, y = half the difference between the screen height and the dialog heght) and use setBounds() to position the dialog.
 
M Burke
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The hide works, thanks!

But I am not sure how to implement the setting of dialog size and position. Do you have a code snippent?
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use setLocationRelativeTo(...) and use the originating frame at the reference. It's in the Window section of the JDialog API.

Cheers,
Rachel
 
P Jain
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

If you have a OK button in that Dialog
In the actionPerformed(ActionEvent ev) write this code
{
String strButton = ev.getActionCommand();
if(strButton.equals("OK"))
{
//write your code of process
setVisible(false);
dispose();
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!