We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,163 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

What package is the window.getSize() ?

hi guys, am stil new to java their was a snippest code about getting screen size i've being seeing onine calling window.getSize() in what package is the object located.

// Get the size of the screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

// Determine the new location of the window
int w = window.getSize().width;                //what package is window.getSize() from?
int h = window.getSize().height;                //what package is window.getSize() from?
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;

// Move the window
window.setLocation(x, y);

it will be helpful if someone can say something

2
Contributors
4
Replies
57 Minutes
Discussion Span
7 Months Ago
Last Updated
5
Views
Question
Answered
hakeemtunde
Junior Poster in Training
50 posts since Dec 2007
Reputation Points: 10
Solved Threads: 8
Skill Endorsements: 2

window is a variable. From the context I guess its a top-level Swing component (JFrame, JDialog, JApplet...)
getSize() is a method that's available for all Swing components. Top-level components inherit it from java.awt.Component. Others inherit it from javax.swing.JComponent.

ps *window.setLocationRelativeTo(null); * does the same thing as that code, but in one line, and it handles multi-screen configurations sensibly.

JamesCherrill
... trying to help
Moderator
8,519 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

Ok. If am to write that code how do i go about it. I mean can you write the code for me so as for me to know is propery instantiated and used in context of the above code

hakeemtunde
Junior Poster in Training
50 posts since Dec 2007
Reputation Points: 10
Solved Threads: 8
Skill Endorsements: 2

It assumes yo have a JFrame or whatever called window. Then when you run the code it moves your window to the centre of the screen. That's all. If your window is called something else, use that name instead of window. You just put the code somewhere in one of your methods, whenever you want the screen to be centered.

JamesCherrill
... trying to help
Moderator
8,519 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

thanks

hakeemtunde
Junior Poster in Training
50 posts since Dec 2007
Reputation Points: 10
Solved Threads: 8
Skill Endorsements: 2
Question Answered as of 7 Months Ago by JamesCherrill

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0775 seconds using 2.69MB