• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Centering JOptionPanes and JDialogs

 
Surya Kumar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to center the JOptionPanes and JDialogs. I want them centered on the screen. Any ideas. Thankxs in advance.
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surya,
How to center the JOptionPanes and JDialogs. I want them centered on the screen. Any ideas. Thankxs in advance.

You can use the awt.Toolkit class to get the screen size, get the screen width and hieght, do some calculation to determine where you want to position your window, then pass this parameter to your object.
It will be something like this:

Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
int screenWidth = screenSize.getScreenWidth();
int screenHieght = screenSize.getScreenHieght();

Then use the width and hieght to position your windows.
 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hanna Habashy:
Surya,

You can use the awt.Toolkit class to get the screen size, get the screen width and hieght, do some calculation to determine where you want to position your window, then pass this parameter to your object.
It will be something like this:

Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
int screenWidth = screenSize.getScreenWidth();
int screenHieght = screenSize.getScreenHieght();

Then use the width and hieght to position your windows.



Its easier than that, the JDialogs and JOptionPanes center on the parent component. If this argument is null they center on the screen. This is dependent on look and feel.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic