Positioning a Frame Core Java

The JFrameclass itself has only a few methods for changing how frames look. Of course,through the magic of inheritance, most of the methods for working with the size and position of a frame come from the various superclasses of JFrame. Here are some of the most important methods:

  • The set Location and set Bounds methods for setting the position of the frame
  • The set Icon Image method, which tells the windowing system which icon to display in the title bar, task switcher window, and so on
  • The set Title method for changing the text in the title bar
  • The set Resizable method, which takes a booleanto determine if a frame will be resizeableby the user

illustrates the inheritance hierarchy for the JFrameclass.

Inheritance hierarchy for the frame and component classes in AWT and Swing

Inheritance hierarchy for the frame and component classes in AWT and Swing

TIP: The API notes for this section give what we think are the most important methods for giving frames the proper look and feel. Some of these methods are defined in the JFrameclass. Others come from the various superclasses of JFrame. At some point, you may need to search the API docs to see if there are methods for some special purpose. Unfortunately, that is a bit tedious to do with inherited methods. For example, the to Front method is applicable to objects of type JFrame, but because it is simply inherited from the Window class, the JFramedocumentation doesn’t explain it. If you feel that there should be a method to do something and it isn’t explained in the documentation for the class you are working with, try looking at the API documentation for the methods of the superclassesof that class. The top of each API page has hyperlinks to the superclasses, and inherited methods are listed below the method summary for the new and overridden methods.

As the API notes indicate, the Component class (which is the ancestor of all GUI objects) and the Window class (which is the superclass of the Frame class) are where you need to look to find the methods to resize and reshape frames. For example, the setLocationmethod in the Component class is one way to reposition a component. If you make the call

setLocation(x, y) the top-left corner is located x pixels across and y pixels down, where (0, 0) is the top -left corner of the screen. Similarly, the set Bounds method in Component lets you resize and relocate a component (in particular, a JFrame) in one step, as

Alternatively, you can give the windowing system control on window placement. If you call

before displaying the window, the windowing system picks the location (but not the size), typically with a slight offset from the last window.

NOTE: For a frame, the coordinates of the setLocationand setBoundsare taken relative to the whole screen. As you will see in Chapter 9, for other components inside a container, the measurements are taken relative to the container.

Frame Properties

Many methods of component classes come in getter /setter pairs, such as the following methods of the Frame class:

Such a getter /setter pair is called a property. A property has a name and a type. The name is obtained by changing the first letter after the get or set to lowercase. For example, the Frame class has a property with name title and type String. Conceptually, title is a property of the frame. When we set the property, we expect that the title changes on the user’s screen. When we get the property, we expect that we get back the value that we set.

We do not know (or care) how the Frame class implements this property. Perhaps it simply uses its peer frame to store the title. Perhaps it has an instance field private String title; // not required for property

If the class does have a matching instance field, we don’t know (or care) how the getter and setter methods are implemented. Perhaps they just read and write the instance field. Perhaps they do more, such as notifying the windowing system whenever the title changes.

There is one exception to the get /set convention: For properties of type boolean, the getter starts with is. For example, the following two methods define the location By Platform property:

NOTE: Many programming languages, in particular, Visual Basic and C#, have built-in supportfor properties. It is possible that a future version of Java will also have a language constructfor properties.

Determining a Good Frame Size

Remember: if you don’t explicitly size a frame, all frames will default to being 0 by 0 pixels. To keep our example programs simple, we resize the frames to a size that we hope works acceptably on most displays. However, in a professional application, you should check the resolution of the user’s screen and write code that resizes the frames accordingly: a window that looks nice on a laptop screen will look like a postage stamp on a high -resolution screen.

To find out the screen size, use the following steps. Call the static get Default Toolkit method of the Toolkit class to get the Toolkit object. (The Toolkit class is a dumping groundfor a variety of methods that interface with the native windowing system.) Then call the get Screen Sizemethod, which returns the screen size as a Dimension object. A Dimension object

simultaneously stores a width and a height, in public (!) instance variables width and height. Here is the code:

We use 50% of these values for the frame size, and tell the windowing system to position the frame:

We also supply an icon. Because the representation of images is also system dependent, we again use the toolkit to load an image. Then, we set the image as the icon for the frame:

Depending on your operating system, you can see the icon in various places. For example, in Windows, the icon is displayed in the top-left corner of the window, and you can see it in the list of active tasks when you press ALT+TAB. Here are a few additional tips for dealing with frames:

  • If your frame contains only standard components such as buttons and text fields, you can simply call the pack method to set the frame size. The frame will be set to the smallest size that contains all components. It is quite common to set the main frame of a program to the maximum size. As of Java SE 1.4, you can simply maximize a frame by calling frame .set Extended State (Frame .MAXIMIZED _BOTH);
  • It is also a good idea to remember how the user positions and sizes the frame of your application and restore those bounds when you start the application again.
  • If you write an application that takes advantage of multiple display screens, use the GraphicsEnvironmentand GraphicsDeviceclasses to find the dimensions of the display screens.
  • The GraphicsDeviceclass also lets you execute your application in full-screen mode.

java.awt.Component 1.0

  • booleanisVisible()
  • void setVisible(boolean b)
    gets or sets the visible property. Components are initially visible, with the exception of top-level components such as JFrame.
  • void setSize(int width, int height) resizes the component to the specified width and height.
  • void setLocation(int x, int y) moves the component to a new location. The x- and y-coordinates use the coordinates of the container if the component is not a top-level component, or the coordinates of the screen if the component is top level (for example, a JFrame).
  • void setBounds(int x, int y, int width, int height)
    moves and resizes this component.


  • Dimension getSize()
  • void setSize(Dimension d)
    gets or sets the size property of this component.
  • void toFront()
    shows this window on top of any other windows.
  • void toBack()
    moves this window to the back of the stack of windows on the desktop and rearranges all other visible windows accordingly.
  • booleanisLocationByPlatform()
  • void setLocationByPlatform(boolean b)
    gets or sets the locationByPlatformproperty. When the property is set before this window is displayed, the platform picks a suitable location.


  • booleanisResizable()
  • void setResizable(boolean b)
    gets or sets the resizable property. When the property is set, the user can resize the frame.
  • String getTitle()
  • void setTitle(String s)
    gets or sets the title property that determines the text in the title bar for the frame.
  • Image getIconImage()
  • void setIconImage(Image image)
    gets or sets the iconImageproperty that determines the icon for the frame. The windowing system may display the icon as part of the frame decoration or in other locations.
  • booleanisUndecorated()>
  • void setUndecorated(boolean b)
    gets or sets the undecorated property. When the property is set, the frame is displayed without decorations such as a title bar or close button. This method must be called before the frame is displayed.
  • intgetExtendedState()
  • void setExtendedState(int state)
    gets or sets the extended window state. The state is one of

java. awt.Toolkit

  • static Toolkit getDefaultToolkit()
  • returns the default toolkit.
  • Dimension getScreenSize()
    gets the size of the user’s screen.
  • Image getImage(String filename)
    loads an image from the file with name filename.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Core Java Topics