devxlogo

Margins in Dialog Boxes

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;	}}
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist