Apr 17, 2012

Insets


If you want to leave a small space between the container that holds your components and the window that contains it. To do this, override the getInsets() method that is defined by the container.
      
          Insets(int top, int left, int bottom, int right)

The values passed in top, left,  bottom and right specify the amount of space between the container and its enclosing window.

Example:

import java.awt.*;
import java.applet.*;


public class InsetsDemo extends Applet
{
 
        public void init()
        {
            setBackground(Color.cyan);
            setLayout(new BorderLayout());
            add(new Button("RMI"),BorderLayout.NORTH);
            add(new Button("SERVLET"),BorderLayout.EAST);
            add(new Button("JDBC"),BorderLayout.SOUTH);
            add(new Button("BEANS"),BorderLayout.WEST);
            add(new Button("JAVA"),BorderLayout.CENTER);
        }


        public Insets getInsets()
        {
            return new Insets(10,20,10,20);
        }
}
/*
<applet code=”InsetsDemo.class” width=300 height=200>
</applet>
*/

How to Execute this Example??

d:\>  javac InsetsDemo.java
d:\>  appletviewer InsetsDemo.java

Output:



4 comments :

  1. Hello I have read this article simple and very nice , however there is some confusion still about insets . can you give me more theoretical example about insets to me . thanks

    ReplyDelete
  2. Hi Mihir Parekh thanks for your feedback. We use insets to leave a space between container and the window.

    To change the applet's insets from the default, we override the insets() method to return a new Insets object, with the new values.

    If you still have a confusion please contact me through the contact form
    http://java06.blogspot.com/p/contact.html

    ReplyDelete
  3. where do you actually call the getinsets method or does the container call it

    ReplyDelete
    Replies
    1. Hi PabloPicasso Container calls the getInsets method internally.

      Delete