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);		addNotify();		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;	}}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: