TableLayout extends AbstractLayout { public void layout(Container container) { Info[] infos = getInfo(container); Rectangle2D bounds = container.getBounds(); Insets2D insets = container.getInsets(); if (insets == null) { insets = new Insets2D.Double(); } Integer lastCol = infos[COLS].size - 1; int compIndex = 0; double x = bounds.getX() + insets.getLeft(); double y = bounds.getY() + insets.getTop(); for (Drawable component : container) { Integer col = compIndex%infos[COLS].size; Integer row = compIndex/infos[COLS].size; double colWidth = infos[COLS].sizes.get(col); double rowHeight = infos[ROWS].sizes.get(row); double w = Math.max(infos[COLS].sizeMean - infos[COLS].unsizeableSpace, colWidth); double h = Math.max(infos[ROWS].sizeMean - infos[ROWS].unsizeableSpace, rowHeight); if (component != null) { component.setBounds(x, y, w, h); } if (col.equals(lastCol)) { x = bounds.getX() + insets.getLeft(); y += h + getGapY(); } else { x += w + getGapX(); } compIndex++; } } TableLayout(int cols, double gapH, double gapV);  TableLayout(int cols); void layout(Container container); Dimension2D getPreferredSize(Container container); int getColumns();  }
@Test public void testLayoutVertical() { Layout layout = new TableLayout(1, GAP_X, GAP_Y); Rectangle2D bounds = new Rectangle2D.Double(5.0, 5.0, 50.0, 50.0); container.setBounds(bounds); layout.layout(container); assertEquals(bounds.getMinX(), a.getX(), DELTA); assertEquals(bounds.getMinX(), b.getX(), DELTA); assertEquals(bounds.getMinX(), c.getX(), DELTA); double meanCompHeight = (bounds.getHeight() - 2.0*GAP_Y)/3.0; assertEquals(bounds.getMinY() + 0.0*meanCompHeight + 0.0*GAP_Y, a.getY(), DELTA); assertEquals(bounds.getMinY() + 1.0*meanCompHeight + 1.0*GAP_Y, b.getY(), DELTA); assertEquals(bounds.getMinY() + 2.0*meanCompHeight + 2.0*GAP_Y, c.getY(), DELTA); }
@Test public void testLayoutHorizontal() { Layout layout = new TableLayout(3, GAP_X, GAP_Y); Rectangle2D bounds = new Rectangle2D.Double(5.0, 5.0, 50.0, 50.0); container.setBounds(bounds); layout.layout(container); double meanCompWidth = (bounds.getWidth() - 2.0*GAP_X)/3.0; assertEquals(bounds.getMinX() + 0.0*meanCompWidth + 0.0*GAP_X, a.getX(), DELTA); assertEquals(bounds.getMinX() + 1.0*meanCompWidth + 1.0*GAP_X, b.getX(), DELTA); assertEquals(bounds.getMinX() + 2.0*meanCompWidth + 2.0*GAP_X, c.getX(), DELTA); assertEquals(bounds.getMinY(), a.getY(), DELTA); assertEquals(bounds.getMinY(), b.getY(), DELTA); assertEquals(bounds.getMinY(), c.getY(), DELTA); }