Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: All
May 15, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Margins in Dialog Boxes

When laying out a dialog box with a manager that does not provide space between the components and the edges of the box (such as a GridLayout manager), the following code will enable you to obtain a margin in a manner which is platform independent. The key is the use of the addNotify() and super.getInsets() methods to obtain the insets that the dialog would have if you did nothing to specify a margin. Then you can add your margins and generate a new insets object to be returned by your class's getInsets() method.
import java.awt.*;
public class YourClass extends Dialog {

	private Insets margins;
	public YourClass(Frame parent) {
		super(parent, "Dialog Title", true);
		Insets border = super.getInsets();
		margins = new Insets(border.top    + 8, border.left  + 8,
		                     border.bottom + 8, border.right + 8);
	// Insert your code here

	public Insets getInsets() {
	return margins;
Tom Yarker
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date