BuddyLayoutAndBorder implements LayoutManager, Border, PropertyChangeListener, UIResource { @Override public Insets getBorderInsets(Component c) { Insets insets = null; if (borderDelegate != null) { insets = (Insets) borderDelegate.getBorderInsets(textField).clone(); } else { insets = new Insets(0, 0, 0, 0); } if (textField == null) { return insets; } for (Component comp : BuddySupport.getLeft(textField)) { insets.left += comp.isVisible() ? comp.getPreferredSize().width : 0; } for (Component comp : BuddySupport.getRight(textField)) { insets.right += comp.isVisible() ? comp.getPreferredSize().width : 0; } Insets outerMargin = BuddySupport.getOuterMargin(textField); if (outerMargin != null) { insets.left += outerMargin.left; insets.right += outerMargin.right; insets.top += outerMargin.top; insets.bottom += outerMargin.bottom; } return insets; }  Border getBorderDelegate(); @Override void addLayoutComponent(String name, Component comp); @Override Dimension minimumLayoutSize(Container parent); @Override Dimension preferredLayoutSize(Container parent); @Override void removeLayoutComponent(Component comp); @Override void layoutContainer(Container parent); @Override Insets getBorderInsets(Component c); Insets getRealBorderInsets(); @Override boolean isBorderOpaque(); @Override void paintBorder(Component c, Graphics g, int x, int y, int width, int height); @Override void propertyChange(PropertyChangeEvent evt); void uninstall(); @Override String toString();  }
@Test public void testBorderInsets() throws Exception { JButton btn = new JButton("hey"); int left = blab.getBorderInsets(textField).left; int btnWidth = btn.getPreferredSize().width; BuddySupport.addLeft(btn, textField); assertSame(btn, BuddySupport.getLeft(textField).get(0)); assertSame(left + btnWidth, blab.getBorderInsets(textField).left); btn.setVisible(false); assertSame(left, blab.getBorderInsets(textField).left); }