Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java Notes: Window Size and Position

 

Learn how to set the Window Size and Position in swing based application.

 

setLocationRelativeTo(null); // Implicit "this" if inside JFrame constructor.


f.setLocationRelativeTo(null); // Explicit JFrame if outside JFrame constructor.

 

First pack your window to do the layout

 

The optimum size of a window depends on the size and layout of the components.

 

After adding all the components to a window (JFrame), call pack() to perform the layout.

 

Centering a Window

 

The following code positions a window (JFrame) f in the center of the screen.

 

Use this when you create a window, after calling pack() has been called, as follows.

setLocationRelativeTo(null);    // Implicit "this" if inside JFrame constructor.
f.setLocationRelativeTo(null);  // Explicit JFrame if outside JFrame constructor.

NetBeans. In the GUI Editor (Matisse) generated source code, add the following statement to the constructor that is automatically generated.

public SortDemoGUI() {  // Matisse generated constructor for SortDemoGUI class.
    initComponents();   // Matisse generated call to initialization method.
    setLocationRelativeTo(null);  // Add this line to center JFrame.
}

You can use the older, more explicit, way of centering a window

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowSize = window.getSize();

int windowX = Math.max(0, (screenSize.width  - windowSize.width ) / 2);
int windowY = Math.max(0, (screenSize.height - windowSize.height) / 2);

f.setLocation(windowX, windowY);  // Don't use "f." inside constructor.

The Math.max call makes sure the window origin is not negative.

Expanding a window to fit the screen

The first example leaves a 4 pixel border around the window. The second example passes the Dimension object directly to setSize(). You don't need the explicit "f." if this code is in the constructor.

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
f.setSize(screenSize.width - 4, screenSize.height - 4);
f.validate();                // Make sure layout is ok

Completely full screen.

f.setSize(Toolkit.getDefaultToolkit().getScreenSize());
f.validate();                // Make sure layout is ok

Fixed Window Size using setSize() and validate() - Probably a mistake

You can set the size of a window, but it's almost always a mistake unless you're simply expanding the window to fill the screen. The default window size should depend on the size and layout of the components as determined by pack(). If you must create a fixed size window (JFrame), call setSize(width, height) or setSize(dimension), then call validate() to finalize the component layout before making the window visible.

Ask programming questions?

 

 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]