How can I find out which component is in which region of a BorderLayout? Is there any way to ask?

Sandip Chitale

Not directly. If all the five components are present with non-zero widths and heights you can deduce that information based on the return value of getBounds() method on each of them (i.e. x, y, width and height) relative to the Container.

Alternatively you can resize the container by 1x1 pixel and compute the changes in the sizes of components.

Thirdly you can examine the private member fields of the BorderLayout instance itself using the Reflection and setting setAccesible(true) and then comparing against the component object references returned by getComponent(i);. Obviously this is a hack. Here is the code snippet from /java/awt/BorderLayout.java


/**
     * Constant to specify components location to be the
     *      north portion of the border layout.
     * @serial
     * @see #getChild
     * @see #addLayoutComponent
     * @see #getLayoutAllignment
     * @see #removeLayoutComponent
     */
	Component north;
     /**
     * Constant to specify components location to be the
     *      west portion of the border layout.
     * @serial
     * @see #getChild
     * @see #addLayoutComponent
     * @see #getLayoutAllignment
     * @see #removeLayoutComponent
     */
	Component west;
    /**
     * Constant to specify components location to be the
     *      east portion of the border layout.
     * @serial
     * @see #getChild
     * @see #addLayoutComponent
     * @see #getLayoutAllignment
     * @see #removeLayoutComponent
     */
	Component east;
    /**
     * Constant to specify components location to be the
     *      south portion of the border layout.
     * @serial
     * @see #getChild
     * @see #addLayoutComponent
     * @see #getLayoutAllignment
     * @see #removeLayoutComponent
     */
    Component south;
    /**
     * Constant to specify components location to be the
     *      center portion of the border layout.
     * @serial
     * @see #getChild
     * @see #addLayoutComponent
     * @see #getLayoutAllignment
     * @see #removeLayoutComponent
     */
	Component center;

[FAQ Manager Note]Another possibility is to subclass the Container with the BorderLayout and watch the added components. For example:

public class ConstraintWatcherPanel extends JPanel {
  private Hashtable componentMap = new Hashtable();
  public void addImpl(Component comp,
                      Object constraint,
                      int index) {
    super(comp,constraint,index);
    componentMap.add(constraint, comp);
  }

  public Component getComponentWithConstraint(Object constraint) {
    return componentMap.get(constraint);
  }

  public Enumeration getConstraints() {
    return componentMap.keys();
  }
}

You would use this as in the following code:


Container p = new ConstraintWatcherPanel();
p.setLayout(new BorderLayout());
p.add(new JButton("A"), BorderLayout.NORTH);
p.add(new JButton("B"), BorderLayout.SOUTH);
p.add(new JButton("C"), BorderLayout.EAST);

Component north = 
  p.getComponentWithConstraint(BorderLayout.NORTH);
Comment and Contribute

 

 

 

 

 


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

 

 

About | Sitemap | Contact