BuddyLayoutAndBorder implements LayoutManager, Border, PropertyChangeListener, UIResource { @Override public Dimension preferredLayoutSize(Container parent) { Dimension d = new Dimension(); for (Component c : BuddySupport.getLeft(textField)) { d.height = Math.max(d.height, c.getPreferredSize().height); } for (Component c : BuddySupport.getRight(textField)) { d.height = Math.max(d.height, c.getPreferredSize().height); } Insets insets = getRealBorderInsets(); d.height += insets.top + insets.bottom; d.width += insets.left + insets.right; Insets outerMargin = BuddySupport.getOuterMargin(textField); if (outerMargin != null) { d.width += outerMargin.left + outerMargin.right; d.height += outerMargin.bottom + outerMargin.top; } return d; }  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 testPreferredWidth() throws Exception { JButton btn = new JButton("hey"); int txtWidth = textField.getPreferredSize().width; assertSame(txtWidth, blab.preferredLayoutSize(textField).width); BuddySupport.addLeft(btn, textField); assertSame(String.format("preferred layout size should be %d", txtWidth), txtWidth, blab.preferredLayoutSize(textField).width); btn.setVisible(false); assertSame(txtWidth, blab.preferredLayoutSize(textField).width); }