Swing Utility Classes

Before we start developing some serious Swing GUIs, it is worth mentioning some utility classes frequently used. They are simple classes. Most of them have some properties specified in their constructors and have getters and setters for those properties.

The Point Class

As the name suggests, an object of the Point class represents a location in a two-dimensional space. Two-dimensional space is represented by two values: an x coordinate and a y coordinate. The Point class is in the java.awt package. The following snippet of code demonstrates its use:

		//Create an object of the Point class with (x, y) coordinate of (20, 40)
		Point p = new Point(20, 40);
		//Get the x and y coordinate of p
		int x = p.getX();
		int y = p.getY();

		//Set the x and y coordinate of p to (10, 60)
		p.setLocation(10, 60);
		The Point class's main usage in Swing is to set and get the location (x and y coordinates) of a component.
		For example, you can set the location of a JButton.
		JButton closeButton = new JButton("Close");
		//The following two statements do the same thing.
		//We will use one of the following statements and not both.
		closeButton.setLocation(10, 15);
		closeButton.setLocation(new Point(10, 15));
		//Get the location of the closeButton
		Point p = closeButton.getLocation();
	
The Dimension Class

An object of the Dimension class wraps the width and height of a component. The width and height of an element are collectively known as its size. In other words, the Dimension class object is used to represent the size of a component. We can use an object of the Dimension class to wrap any two arbitrary integers. The class is in the java.awt package.

		//Create an object of the Dimension class with a width and height of 200 and 20
		Dimension d = new Dimension(200, 20);
		//Set the size of closeButton to 200 X 20. Both of the statements have the same efecct.
		//You will use one of the following two statements.
		closeButton.setSize(200, 20);
		closeButton.setsize(d);

		//Get the size of closeButton
		Dimension d2 = closeButton.getSize();
		int width = d2.width;
		int height = d2.height;
	
The Insets Class

An object of the Insets class represents spaces that are left around a container. It wraps four properties named top, left, bottom, and right. Their values represent the spaces left on the four side of a container. The class is in the java.awt package.

		//Create an object of the Insets class
		//using its constructor Insets(top, left, bottom, right)
		Insets ins = new Insets(20, 5, 5, 5);
		//Get the insets of a JFrame
		Insets ins = frame.getInsets();
		int top = ins.top;
		int left = ins.left;
		int bottom = ins.bottom;
		int right = ins.right;
	
The Rectangle Class

As its name suggests, an instance of the Rectangle class represents a rectangle. It is in the java.awt package. You can define a rectangle in many ways. Three properties define a Rectangle:

  • (x, y) coordinates of the upper-left corner
  • Width
  • Height

We can think of a Rectangle object as a combination of a Point object and a Dimension object; the Point object has the (x, y) coordinates of the upper left corner and the Dimension object holds the width and height. We can create an object of the Rectangle class by specifying different combinations of its properties.

		//Create a Rectangle object whose upper-left corner is at (0, 0)
		//with width and height as zero
		Rectangle r1 = new Rectangle();

		//Create a Rectangle object from a Point object with its width and height as zero
		Rectangle r2 = new Rectangle(new Point(10, 10));

		//Create a Rectangle object from a Point object and a Dimension object
		Rectangle r3 = new Rectangle(new Point(10, 10), new Dimension(200, 100));

		//Create a Rectangle object by specifying its upper-left corner's
		//coordinate at (10, 10) and width as 200 and height as 100
		Rectangle r4 = new Rectangle(10, 10, 200, 100);
	

The Rectangle class defines many methods to manipulate a Rectangle object and to inquire about its properties, such as the (x, y) coordinate of its upper-left corner, width, and height.

An object of the Rectangle class defines the location and size of a component in a Swing application. The location and size of a component are known as its bounds. Two methods, setBounds() and getBounds(), can be used to set and get any component or container’s bounds. The setBounds() method is overloaded, and you can specify x, y, width, and height properties of a component, or a Rectangle object. The getBounds() method returns a Rectangle object.

Copyright 1999- Ducat Creative, All rights reserved.