public void setColumnWidths() { // See // "http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#custom" int n = getModel().getColumnCount(); for (int j = 0; j < n; ++j) { TableColumn column = getColumnModel().getColumn(j); TableCellRenderer headerRenderer = column.getHeaderRenderer(); if (headerRenderer == null) headerRenderer = getTableHeader().getDefaultRenderer(); // the new 1.3 way Component columnComponent = headerRenderer.getTableCellRendererComponent( this, column.getHeaderValue(), false, false, -1, j); Component cellComponent = getDefaultRenderer(getColumnClass(j)) .getTableCellRendererComponent(this, getModel().getValueAt(0, j), false, false, 0, j); int wantWidth = Math.max( columnComponent.getPreferredSize().width + 10, // fudge factor ... seems to always be too small otherwise (on x86 Linux) cellComponent.getPreferredSize().width + 10 // fudge factor ... seems to always be too small otherwise (on Mac OS X) ); column.setPreferredWidth(wantWidth); } }
/** * Calculates the preferred size dimensions for the specified panel given the components in the * specified parent container. * * @param target The component to be laid out. * @return A size deemed suitable for laying out the container. * @see #minimumLayoutSize */ public Dimension preferredLayoutSize(Container target) { int count; Component component; Dimension dimension; Insets insets; Dimension ret; synchronized (target.getTreeLock()) { // get the the total height and maximum width component ret = new Dimension(0, 0); count = target.getComponentCount(); for (int i = 0; i < count; i++) { component = target.getComponent(i); if (component.isVisible()) { dimension = component.getPreferredSize(); ret.width = Math.max(ret.width, dimension.width); ret.height += dimension.height; } } insets = target.getInsets(); ret.width += insets.left + insets.right; ret.height += insets.top + insets.bottom; } return (ret); }
/** * Lays out the container. * * @param target The container which needs to be laid out. */ public void layoutContainer(Container target) { int count; Component component; Dimension dimension; synchronized (target.getTreeLock()) { count = target.getComponentCount(); for (int i = 0; i < count; i++) { component = target.getComponent(i); if (component.isVisible()) { dimension = component.getPreferredSize(); component.setSize(dimension.width, dimension.height); } } } }
/** * Calculates the preferred size dimensions for the specified panel given the components in the * specified parent container. * * @see #minimumLayoutSize * @param target The component to be laid out. * @return A size deemed suitable for laying out the container. */ public Dimension preferredLayoutSize(Container target) { int count; Container parent; Component component; Point point; Dimension dimension; Insets insets; Dimension ret; synchronized (target.getTreeLock()) { count = target.getComponentCount(); if (0 == count) { // be the same size unless we have a parent ret = target.getSize(); parent = target.getParent(); if (null != parent) { insets = parent.getInsets(); ret = parent.getSize(); ret.setSize( ret.width - insets.left - insets.right, ret.height - insets.top - insets.bottom); } } else { ret = new Dimension(0, 0); for (int i = 0; i < count; i++) { component = target.getComponent(i); if (component.isVisible()) { point = component.getLocation(); dimension = component.getPreferredSize(); ret.width = Math.max(ret.width, point.x + dimension.width); ret.height = Math.max(ret.height, point.y + dimension.height); } } insets = target.getInsets(); ret.width += insets.left + insets.right; ret.height += insets.top + insets.bottom; } } return (ret); }
/** * Lays out the container. * * @param target The container which needs to be laid out. */ public void layoutContainer(Container target) { Insets insets; int x; int y; int count; int width; Component component; Dimension dimension; synchronized (target.getTreeLock()) { insets = target.getInsets(); x = insets.left; y = insets.top; count = target.getComponentCount(); width = 0; for (int i = 0; i < count; i++) { component = target.getComponent(i); if (component.isVisible()) { dimension = component.getPreferredSize(); width = Math.max(width, dimension.width); component.setSize(dimension.width, dimension.height); component.setLocation(x, y); y += dimension.height; } } // now set them all to the same width for (int i = 0; i < count; i++) { component = target.getComponent(i); if (component.isVisible()) { dimension = component.getSize(); dimension.width = width; component.setSize(dimension.width, dimension.height); } } } }
/** * Sets the scaled component height, adopting the original component width. * * @param comp component * @param h height */ public static void setHeight(final Component comp, final int h) { comp.setPreferredSize(new Dimension(comp.getPreferredSize().width, (int) (h * scale))); }
/** * Sets the scaled component width, adopting the original component height. * * @param comp component * @param w width */ public static void setWidth(final Component comp, final int w) { comp.setPreferredSize(new Dimension((int) (w * scale), comp.getPreferredSize().height)); }