• Post Reply Bookmark Topic Watch Topic
  • New Topic

Windows in SWT  RSS feed

 
Machine_Head
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i create centered windows (to set shells position centered) in SWT,
can somebody help me???

thanks
 
sa sam
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JFrame frame = (JFrame) new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

// Get the screen size
GraphicsConfiguration gc = frame.getGraphicsConfiguration();
Rectangle bounds = gc.getBounds();

// Create and pack the Elements
//.... code to create the panels etc
frame.pack();

// Set the Location and Activate
Dimension size = frame.getPreferredSize();
frame.setLocation((int) ((bounds.width / 2) - (size.getWidth() / 2)),
(int) ((bounds.height / 2) - (size.getHeight() / 2)));
frame.setVisible(true);
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ajay Chavan, welcome to JavaRanch

Thank you for the reply, but it might be a little late after 4 years: look at this FAQ.
 
sa sam
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks



it will help others.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well not really.
1) The original poster was referring to SWT. I am not sure your solution would be applicable there.
2) You really do not need to calculate the bounds and figure out the x,y co-ordinates. Just one line will do the trick
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!