Articles Projects Tips Downloads Contacts About

View's rectangle in JEditorPane.

The method is based on child allocations started from root view till the past view reached. All coordinates are summed to get view's location. Height and width are preferred ones for the view

    public static Shape getAllocation(View v, JEditorPane edit) {
        Insets ins=edit.getInsets();
        View vParent=v.getParent();
        int x=ins.left;
        while(vParent!=null) {

            int i=vParent.getViewIndex(v.getStartOffset(), Position.Bias.Forward);
            Shape alloc=vParent.getChildAllocation(i, new Rectangle(0,0, Short.MAX_VALUE, Short.MAX_VALUE));
        if (v instanceof BoxView) {

            int ind=v.getParent().getViewIndex(v.getStartOffset(), Position.Bias.Forward);
            Rectangle r2=v.getParent().getChildAllocation(ind, 
                      new Rectangle(0,0,Integer.MAX_VALUE,Integer.MAX_VALUE)).getBounds();
            return new Rectangle(x,y, r2.width, r2.height);

        return new Rectangle(x,y, (int)v.getPreferredSpan(View.X_AXIS), (int)v.getPreferredSpan(View.Y_AXIS));