diff --git a/examples/src/main/java/edu/umd/cs/piccolo/examples/SwingLayoutExample.java b/examples/src/main/java/edu/umd/cs/piccolo/examples/SwingLayoutExample.java index 5876634..cd01e3f 100644 --- a/examples/src/main/java/edu/umd/cs/piccolo/examples/SwingLayoutExample.java +++ b/examples/src/main/java/edu/umd/cs/piccolo/examples/SwingLayoutExample.java @@ -121,8 +121,9 @@ gridBagLayoutNode.setOffset(400, 250); rootNode.addChild(gridBagLayoutNode); - final SwingLayoutNode boxLayoutNode = new SwingLayoutNode(); - boxLayoutNode.setLayout(new BoxLayout(boxLayoutNode.getContainer(), BoxLayout.Y_AXIS)); + JPanel container = new JPanel(); + container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS)); + final SwingLayoutNode boxLayoutNode = new SwingLayoutNode(container); boxLayoutNode.addChild(new MyPPath(new Rectangle2D.Double(0, 0, 50, 50), Color.yellow, new BasicStroke(2), Color.red)); boxLayoutNode.addChild(new MyPPath(new Rectangle2D.Double(0, 0, 100, 50), Color.orange, new BasicStroke(2), diff --git a/extras/src/main/java/edu/umd/cs/piccolox/swing/SwingLayoutNode.java b/extras/src/main/java/edu/umd/cs/piccolox/swing/SwingLayoutNode.java index 369dfd4..f9309e5 100644 --- a/extras/src/main/java/edu/umd/cs/piccolox/swing/SwingLayoutNode.java +++ b/extras/src/main/java/edu/umd/cs/piccolox/swing/SwingLayoutNode.java @@ -38,6 +38,7 @@ import java.util.Collection; import java.util.Iterator; +import javax.swing.BoxLayout; import javax.swing.JComponent; import javax.swing.JPanel; @@ -63,7 +64,7 @@ private static final Anchor DEFAULT_ANCHOR = Anchor.WEST; /** Container for ProxyComponents. */ - private final JPanel container; + private final Container container; private final PropertyChangeListener propertyChangeListener; @@ -85,7 +86,21 @@ * not be null. */ public SwingLayoutNode(final LayoutManager layoutManager) { - container = new JPanel(layoutManager); + this(new JPanel(layoutManager)); + } + + /** + * Constructs a SwingLayoutNode that lays out its children as though they + * were children of the provided Container. + * + * Whatever LayoutManager is being used by the container will be used when + * laying out nodes. + * + * @param container Container in which child nodes will effectively be laid + * out + */ + public SwingLayoutNode(Container container) { + this.container = container; propertyChangeListener = new PropertyChangeListener() { public void propertyChange(final PropertyChangeEvent event) { final String propertyName = event.getPropertyName(); @@ -131,7 +146,7 @@ */ public Container getContainer() { return container; - } + } /** * Adds a child at the specified index. Like Swing, bad things can happen if