just wrote this for my little app in progress (highscore server). nothing fancy but it has it's place in some tool package. i just use it to center my JFrame and other stuff.
1 2 3 4 5 6 7 8 9 10
| public void centerComponent(Component c) { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice device = env.getDefaultScreenDevice(); DisplayMode displayMode = device.getDisplayMode(); int screenWidth = displayMode.getWidth(); int screenHeight = displayMode.getHeight(); int componentWidth = c.getWidth(); int componentHeight = c.getHeight(); c.setLocation(screenWidth/2 - componentWidth/2, screenHeight/2 - componentHeight/2); } |