Java Code Examples for java.awt.Component#getPreferredSize()

The following examples show how to use java.awt.Component#getPreferredSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LineLayout.java    From oim-fx with MIT License 10 votes vote down vote up
private void layoutContainerH(Container target) {
    int maxHeight = 0;
    Dimension size;
    int height;
    Insets insets = target.getInsets();

    for (Component component : target.getComponents()) {
        if (component.isVisible()) {
            size = component.getPreferredSize();

            if (fillComponents.contains(component)) {
                height = target.getHeight() - insets.top - insets.bottom - topGap - bottomGap;
            } else {
                height = size.height;

                if (height > maxHeight) {
                    maxHeight = height;
                }
            }

            component.setSize(size.width, height);
        }
    }

    moveComponentsH(target, maxHeight);
}
 
Example 2
Source File: VerticalLayout.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
public Dimension preferredLayoutSize(final Container parent) {
    final Insets insets = parent.getInsets();
    final Dimension d = new Dimension(insets.left + insets.right,
                                      insets.top + insets.bottom);
    int maxWidth = 0;
    int visibleCount = 0;

    for (Component comp : parent.getComponents()) {
        if (comp.isVisible()) {
            final Dimension size = comp.getPreferredSize();
            maxWidth = Math.max(maxWidth, size.width);
            d.height += size.height;
            visibleCount++;
        }
    }

    d.height += (visibleCount - 1) * vGap;
    d.width += maxWidth;

    return d;
}
 
Example 3
Source File: VerticalLayout.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
public void layoutContainer(final Container parent) {
    final Insets insets = parent.getInsets();
    final int posX = insets.left;
    int posY = insets.top;
    final int width = parent.getWidth() - insets.left - insets.right;

    for (Component comp : parent.getComponents()) {
        if (comp.isVisible()) {
            Dimension pref = comp.getPreferredSize();
            if (proportionalWidth) {
                int w = Math.min(pref.width, width);
                int o = (width - w) / 2;
                comp.setBounds(posX, posY + o, w, pref.height);
            } else {
                comp.setBounds(posX, posY, width, pref.height);
            }
            pref.height += vGap;
            posY += pref.height;
        }
    }
}
 
Example 4
Source File: VerticalLayout.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
public Dimension preferredLayoutSize(final Container parent) {
    final Insets insets = parent.getInsets();
    final Dimension d = new Dimension(insets.left + insets.right,
                                      insets.top + insets.bottom);
    int maxWidth = 0;
    int visibleCount = 0;

    for (Component comp : parent.getComponents()) {
        if (comp.isVisible()) {
            final Dimension size = comp.getPreferredSize();
            maxWidth = Math.max(maxWidth, size.width);
            d.height += size.height;
            visibleCount++;
        }
    }

    d.height += (visibleCount - 1) * vGap;
    d.width += maxWidth;

    return d;
}
 
Example 5
Source File: DimensionEncapsulation.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public void test(final Component component) {
    final Dimension psize = component.getPreferredSize();
    psize.width += 200;
    if (Objects.equals(psize, component.getPreferredSize())) {
        throw new RuntimeException("PreferredSize is wrong");
    }
    final Dimension msize = component.getMaximumSize();
    msize.width += 200;
    if (Objects.equals(msize, component.getMaximumSize())) {
        throw new RuntimeException("MaximumSize is wrong");
    }
    final Dimension misize = component.getMinimumSize();
    misize.width += 200;
    if (Objects.equals(misize, component.getMinimumSize())) {
        throw new RuntimeException("MinimumSize is wrong");
    }
}
 
Example 6
Source File: VerticalLayout.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public Dimension minimumLayoutSize(final Container parent) {
    final Insets insets = parent.getInsets();
    final Dimension d = new Dimension(insets.left + insets.right,
                                      insets.top + insets.bottom);
    int maxWidth = 0;
    int visibleCount = 0;

    for (Component comp : parent.getComponents()) {
        if (comp.isVisible() && !(comp instanceof Box.Filler)) {
            final Dimension size = comp.getPreferredSize();
            maxWidth = Math.max(maxWidth, size.width);
            d.height += size.height;
            visibleCount++;
        }
    }

    d.height += (visibleCount - 1) * vGap;
    d.width += maxWidth;

    return d;
}
 
Example 7
Source File: SwingUtilities2.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if the given point is outside the preferredSize of the
 * item at the given row of the table.  (Column must be 0).
 * Does not check the "Table.isFileList" property. That should be checked
 * before calling this method.
 * This is used to make Windows {@literal L&F} JFileChooser act
 * like native dialogs.
 */
public static boolean pointOutsidePrefSize(JTable table, int row, int column, Point p) {
    if (table.convertColumnIndexToModel(column) != 0 || row == -1) {
        return true;
    }
    TableCellRenderer tcr = table.getCellRenderer(row, column);
    Object value = table.getValueAt(row, column);
    Component cell = tcr.getTableCellRendererComponent(table, value, false,
            false, row, column);
    Dimension itemSize = cell.getPreferredSize();
    Rectangle cellBounds = table.getCellRect(row, column, false);
    cellBounds.width = itemSize.width;
    cellBounds.height = itemSize.height;

    // See if coords are inside
    // ASSUME: mouse x,y will never be < cell's x,y
    assert (p.x >= cellBounds.x && p.y >= cellBounds.y);
    return p.x > cellBounds.x + cellBounds.width ||
            p.y > cellBounds.y + cellBounds.height;
}
 
Example 8
Source File: MetalworksPrefs.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public Dimension minimumLayoutSize(Container c) {
    Insets insets = c.getInsets();
    int height = yInset + insets.top;
    int width = 0 + insets.left + insets.right;

    Component[] children = c.getComponents();
    Dimension compSize = null;
    for (Component child : children) {
        compSize = child.getPreferredSize();
        height += compSize.height + yGap;
        width = Math.max(width, compSize.width + insets.left + insets.right + xInset
                * 2);
    }
    height += insets.bottom;
    return new Dimension(width, height);
}
 
Example 9
Source File: CenterLayout.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the preferred size.
 *
 * @param parent  the parent.
 *
 * @return the preferred size.
 */
public Dimension preferredLayoutSize(final Container parent) {

    synchronized (parent.getTreeLock()) {
        final Insets insets = parent.getInsets();
        if (parent.getComponentCount() > 0) {
            final Component component = parent.getComponent(0);
            final Dimension d = component.getPreferredSize();
            return new Dimension(
                (int) d.getWidth() + insets.left + insets.right,
                (int) d.getHeight() + insets.top + insets.bottom
            );
        }
        else {
            return new Dimension(
                insets.left + insets.right, insets.top + insets.bottom
            );
        }
    }

}
 
Example 10
Source File: TitledMenuSeparator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void doLayout() {
    super.doLayout();
    Component c = getComponent(1);
    
    int h = c.getPreferredSize().height;
    Rectangle b = c.getBounds();
    
    b.y = (b.height - h) / 2;
    b.height = h;
    c.setBounds(b);
}
 
Example 11
Source File: XMBeanAttributes.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
MaximizedCellRenderer(Component comp) {
    this.comp = comp;
    Dimension d = comp.getPreferredSize();
    if (d.getHeight() > 220) {
        comp.setPreferredSize(new Dimension((int) d.getWidth(), 220));
    }
}
 
Example 12
Source File: CompactLayout.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lays out the specified container.
 * @param parent the container to be laid out
 */
public void layoutContainer(Container parent) {
    int n = parent.getComponentCount();
    Insets insets = parent.getInsets();
    Dimension size = parent.getSize();
    int c = horizontal ? insets.left : insets.top;
    int x, y;
    int ebx = size.width - insets.right;
    size.width -= (insets.left + insets.right);
    size.height -= (insets.top + insets.bottom);
    for (int i = 0; i < n; i++) {
        Component comp = parent.getComponent(i);
        Dimension pref = comp.getPreferredSize();
        if (comp instanceof EnableButton) {
            ebx -= 4;
            ebx -= pref.width;
            x = ebx;
            y = (insets.top - pref.height) / 2;
        } else if (horizontal) {
            x = c;
            c += pref.width;
            y = insets.top;
            pref.height = size.height;
        } else {
            x = insets.left;
            pref.width = size.width;
            y = c;
            c += pref.height;
        }
        comp.setBounds(x, y, pref.width, pref.height);
    }
}
 
Example 13
Source File: CompactLayout.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public Dimension getSize(Container parent, boolean minimum) {
    int n = parent.getComponentCount();
    Insets insets = parent.getInsets();
    Dimension d = new Dimension();
    for (int i = 0; i < n; i++) {
        Component comp = parent.getComponent(i);
        if (comp instanceof EnableButton) {
            continue;
        }
        Dimension p = (minimum
                       ? comp.getMinimumSize()
                       : comp.getPreferredSize());
        if (horizontal) {
            d.width += p.width;
            if (d.height < p.height) {
                d.height = p.height;
            }
        } else {
            if (d.width < p.width) {
                d.width = p.width;
            }
            d.height += p.height;
        }
    }
    d.width += (insets.left + insets.right);
    d.height += (insets.top + insets.bottom);
    return d;
}
 
Example 14
Source File: XMBeanAttributes.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
MaximizedCellRenderer(Component comp) {
    this.comp = comp;
    Dimension d = comp.getPreferredSize();
    if (d.getHeight() > 220) {
        comp.setPreferredSize(new Dimension((int) d.getWidth(), 220));
    }
}
 
Example 15
Source File: GuiUtilities.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Set the location of a component to center it on the screen.
 * 
 * @param component
 *            the component to center.
 */
public static void centerOnScreen( Component component ) {
    Dimension prefSize = component.getPreferredSize();
    Dimension parentSize;
    java.awt.Point parentLocation = new java.awt.Point(0, 0);
    parentSize = Toolkit.getDefaultToolkit().getScreenSize();
    int x = parentLocation.x + (parentSize.width - prefSize.width) / 2;
    int y = parentLocation.y + (parentSize.height - prefSize.height) / 2;
    component.setLocation(x, y);
}
 
Example 16
Source File: ViewportLayout.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the preferred dimensions for this layout given the components
 * in the specified target container.
 * @param parent the component which needs to be laid out
 * @return a <code>Dimension</code> object containing the
 *          preferred dimensions
 * @see #minimumLayoutSize
 */
public Dimension preferredLayoutSize(Container parent) {
    Component view = ((JViewport)parent).getView();
    if (view == null) {
        return new Dimension(0, 0);
    }
    else if (view instanceof Scrollable) {
        return ((Scrollable)view).getPreferredScrollableViewportSize();
    }
    else {
        return view.getPreferredSize();
    }
}
 
Example 17
Source File: MetalworksPrefs.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void layoutContainer(Container c) {
    Insets insets = c.getInsets();
    int height = yInset + insets.top;

    Component[] children = c.getComponents();
    Dimension compSize = null;
    for (Component child : children) {
        compSize = child.getPreferredSize();
        child.setSize(compSize.width, compSize.height);
        child.setLocation(xInset + insets.left, height);
        height += compSize.height + yGap;
    }

}
 
Example 18
Source File: VerticalFlowLayout.java    From Method_Trace_Tool with Apache License 2.0 4 votes vote down vote up
/**
 * Lays out the container.
 *
 * @param target
 *            the container to lay out.
 */
public void layoutContainer(Container target) {
	Insets insets = target.getInsets();
	int maxheight = target.getSize().height
			- (insets.top + insets.bottom + vgap * 2);
	int maxwidth = target.getSize().width
			- (insets.left + insets.right + hgap * 2);
	int numcomp = target.getComponentCount();
	int x = insets.left + hgap, y = 0;
	int colw = 0, start = 0;

	for (int i = 0; i < numcomp; i++) {
		Component m = target.getComponent(i);
		if (m.isVisible()) {
			Dimension d = m.getPreferredSize();
			// fit last component to remaining height
			if ((this.vfill) && (i == (numcomp - 1))) {
				d.height = Math.max((maxheight - y),
						m.getPreferredSize().height);
			}

			// fit component size to container width
			if (this.hfill) {
				m.setSize(maxwidth, d.height);
				d.width = maxwidth;
			} else {
				m.setSize(d.width, d.height);
			}

			if (y + d.height > maxheight) {
				placethem(target, x, insets.top + vgap, colw,
						maxheight - y, start, i);
				y = d.height;
				x += hgap + colw;
				colw = d.width;
				start = i;
			} else {
				if (y > 0)
					y += vgap;
				y += d.height;
				colw = Math.max(colw, d.width);
			}
		}
	}
	placethem(target, x, insets.top + vgap, colw, maxheight - y, start,
			numcomp);
}
 
Example 19
Source File: Test6524757.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void addSize(List<Object> list, Component component, int x, int y, int w, int h) {
    Dimension size = component.getPreferredSize();
    int width = (size.width + 1) / w - x;
    int height = (size.height + 1) / h - y;
    list.add(new Dimension(width, height));
}
 
Example 20
Source File: InnerPanelSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private boolean updateTooltipImage(RenderedImage contents, int row, int col) {
            TableCellRenderer renderer = listClasses.getCellRenderer(row, col); 
            Component component = listClasses.prepareRenderer(renderer, row, col);
            
            if (!(component instanceof JComponent)) {
                // sorry.
                return false;
            }
            Rectangle cellRect = listClasses.getCellRect(row, col, false);
            Dimension size = component.getPreferredSize();
            Rectangle visibleRect = listClasses.getVisibleRect();
            
            // The visible region is wide enough, hide the tooltip
            if (cellRect.width >= size.width) {
                hidePopup();
                return false;
            }           
            // Hide if the cell does not vertically fit
            if (cellRect.y + size.height > visibleRect.y + visibleRect.height + 1) {
                hidePopup();
                return false;
            }
            BufferedImage img = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
            Graphics g = img.getGraphics();
            g.setClip(null);
            g.setColor(listClasses.getBackground());
            g.fillRect(0, 0, size.width, size.height);
            g.translate(-cellRect.x, -cellRect.y);
//            g.translate(-visibleRect.x, -visibleRect.y);
            
            cellRect.width = size.width;
            // prevent some repaing issues, see javadoc for CellRendererPane.
            rendererPane.paintComponent(g, component, listClasses, cellRect);
            
            // if table displays lines, frame the cell's display using lines.
            if (listClasses.getShowHorizontalLines()) {
                int rightX = size.width - 1;
                g.translate(cellRect.x, cellRect.y);
                g.setColor(listClasses.getForeground());
                g.drawLine(0, 0, rightX, 0);
                g.drawLine(rightX, 0, rightX, size.height);
                g.drawLine(rightX, size.height - 1, 0, size.height - 1);
            }
            g.dispose();
            rendererPane.remove(component);
            contents.setImage(img);
            return true;
        }