Java Code Examples for java.awt.Container#getWidth()

The following examples show how to use java.awt.Container#getWidth() . 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: GroupLayout.java    From openjdk-8-source with GNU General Public License v2.0 8 votes vote down vote up
/**
 * Lays out the specified container.
 *
 * @param parent the container to be laid out
 * @throws IllegalStateException if any of the components added to
 *         this layout are not in both a horizontal and vertical group
 */
public void layoutContainer(Container parent) {
    // Step 1: Prepare for layout.
    prepare(SPECIFIC_SIZE);
    Insets insets = parent.getInsets();
    int width = parent.getWidth() - insets.left - insets.right;
    int height = parent.getHeight() - insets.top - insets.bottom;
    boolean ltr = isLeftToRight();
    if (getAutoCreateGaps() || getAutoCreateContainerGaps() ||
            hasPreferredPaddingSprings) {
        // Step 2: Calculate autopadding springs
        calculateAutopadding(horizontalGroup, HORIZONTAL, SPECIFIC_SIZE, 0,
                width);
        calculateAutopadding(verticalGroup, VERTICAL, SPECIFIC_SIZE, 0,
                height);
    }
    // Step 3: set the size of the groups.
    horizontalGroup.setSize(HORIZONTAL, 0, width);
    verticalGroup.setSize(VERTICAL, 0, height);
    // Step 4: apply the size to the components.
    for (ComponentInfo info : componentInfos.values()) {
        info.setBounds(insets, width, ltr);
    }
}
 
Example 2
Source File: GroupLayout.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Lays out the specified container.
 *
 * @param parent the container to be laid out
 * @throws IllegalStateException if any of the components added to
 *         this layout are not in both a horizontal and vertical group
 */
public void layoutContainer(Container parent) {
    // Step 1: Prepare for layout.
    prepare(SPECIFIC_SIZE);
    Insets insets = parent.getInsets();
    int width = parent.getWidth() - insets.left - insets.right;
    int height = parent.getHeight() - insets.top - insets.bottom;
    boolean ltr = isLeftToRight();
    if (getAutoCreateGaps() || getAutoCreateContainerGaps() ||
            hasPreferredPaddingSprings) {
        // Step 2: Calculate autopadding springs
        calculateAutopadding(horizontalGroup, HORIZONTAL, SPECIFIC_SIZE, 0,
                width);
        calculateAutopadding(verticalGroup, VERTICAL, SPECIFIC_SIZE, 0,
                height);
    }
    // Step 3: set the size of the groups.
    horizontalGroup.setSize(HORIZONTAL, 0, width);
    verticalGroup.setSize(VERTICAL, 0, height);
    // Step 4: apply the size to the components.
    for (ComponentInfo info : componentInfos.values()) {
        info.setBounds(insets, width, ltr);
    }
}
 
Example 3
Source File: GroupLayout.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Lays out the specified container.
 *
 * @param parent the container to be laid out
 * @throws IllegalStateException if any of the components added to
 *         this layout are not in both a horizontal and vertical group
 */
public void layoutContainer(Container parent) {
    // Step 1: Prepare for layout.
    prepare(SPECIFIC_SIZE);
    Insets insets = parent.getInsets();
    int width = parent.getWidth() - insets.left - insets.right;
    int height = parent.getHeight() - insets.top - insets.bottom;
    boolean ltr = isLeftToRight();
    if (getAutoCreateGaps() || getAutoCreateContainerGaps() ||
            hasPreferredPaddingSprings) {
        // Step 2: Calculate autopadding springs
        calculateAutopadding(horizontalGroup, HORIZONTAL, SPECIFIC_SIZE, 0,
                width);
        calculateAutopadding(verticalGroup, VERTICAL, SPECIFIC_SIZE, 0,
                height);
    }
    // Step 3: set the size of the groups.
    horizontalGroup.setSize(HORIZONTAL, 0, width);
    verticalGroup.setSize(VERTICAL, 0, height);
    // Step 4: apply the size to the components.
    for (ComponentInfo info : componentInfos.values()) {
        info.setBounds(insets, width, ltr);
    }
}
 
Example 4
Source File: DefaultTabbedContainerUI.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void layoutContainer(Container container) {
    Dimension tabSize = tabDisplayer.getPreferredSize();
    Insets ins = container.getInsets();
    int w = container.getWidth() - (ins.left + ins.right);
    tabDisplayer.setBounds (ins.left, ins.top, 
        w,
        tabSize.height);
    if( tabDisplayer.getModel().size() > 1 ) {
        //#214427 - check the preferred size again, during the first pass
        //the tab displayer may not know the available width
        Dimension newTabSize = tabDisplayer.getPreferredSize();
        if( newTabSize.height != tabSize.height ) {
            tabSize = newTabSize;
            tabDisplayer.setBounds (ins.left, ins.top,
                w,
                tabSize.height);
        }
    }
    contentDisplayer.setBounds(ins.left, 
        ins.top + tabSize.height, w,
        container.getHeight() - (ins.top + ins.bottom + tabSize.height));
}
 
Example 5
Source File: GroupLayout.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Lays out the specified container.
 *
 * @param parent the container to be laid out
 * @throws IllegalStateException if any of the components added to
 *         this layout are not in both a horizontal and vertical group
 */
public void layoutContainer(Container parent) {
    // Step 1: Prepare for layout.
    prepare(SPECIFIC_SIZE);
    Insets insets = parent.getInsets();
    int width = parent.getWidth() - insets.left - insets.right;
    int height = parent.getHeight() - insets.top - insets.bottom;
    boolean ltr = isLeftToRight();
    if (getAutoCreateGaps() || getAutoCreateContainerGaps() ||
            hasPreferredPaddingSprings) {
        // Step 2: Calculate autopadding springs
        calculateAutopadding(horizontalGroup, HORIZONTAL, SPECIFIC_SIZE, 0,
                width);
        calculateAutopadding(verticalGroup, VERTICAL, SPECIFIC_SIZE, 0,
                height);
    }
    // Step 3: set the size of the groups.
    horizontalGroup.setSize(HORIZONTAL, 0, width);
    verticalGroup.setSize(VERTICAL, 0, height);
    // Step 4: apply the size to the components.
    for (ComponentInfo info : componentInfos.values()) {
        info.setBounds(insets, width, ltr);
    }
}
 
Example 6
Source File: UI.java    From Forsythia with GNU General Public License v3.0 6 votes vote down vote up
UI(Strobe strobe){
  this.strobe=strobe;
  setBounds(400,50,100,100);
  Container c=this.getContentPane();
  int 
    xfat=100-c.getHeight(),
    yfat=100-c.getWidth();
  
  //1080i is 1920 horizontal and 540 vertical pixels
  
  
  setBounds(400,50,1280+xfat,720+yfat);
  setVisible(true);
  setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  
}
 
Example 7
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 + o, posY, w, pref.height);
            } else {
                comp.setBounds(posX, posY, width, pref.height);
            }
            pref.height += vGap;
            posY += pref.height;
        }
    }
}
 
Example 8
Source File: NbEditorUI.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean getScrollableTracksViewportWidth() {
    Container parent = SwingUtilities.getUnwrappedParent(this);
    if (parent instanceof JViewport) {
        return parent.getWidth() > getPreferredSize().width;
    }
    return false;
}
 
Example 9
Source File: SplayedLayout.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
public void layoutContainer(Container parent) {
	JComponent[] components = getComponents(parent);
	if (components.length == 0) {
		return;
	}

	Border border = ((JComponent) parent).getBorder();
	Insets i = border == null ? new Insets(0, 0, 0, 0) : border
			.getBorderInsets(parent);
	int x = i.left;
	int y = i.top;
	int width = parent.getWidth() - i.left - i.right;
	int height = parent.getHeight() - i.top - i.bottom;

	int separators = components.length - 1;
	int requestedSize = horizontal ? separators * separatorSize.width
			: separators * separatorSize.height;
	LinkedHashMap<JComponent, Dimension> preferredSize = new LinkedHashMap<>(
			components.length);
	for (JComponent c : components) {
		Dimension d = c.getPreferredSize();
		preferredSize.put(c, d);
		requestedSize += horizontal ? d.width : d.height;
	}

	int max = horizontal ? width : height;
	Plan plan;
	if (requestedSize <= max) {
		plan = layoutContainerWithExtra((JComponent) parent, preferredSize,
				requestedSize, x, y, width, height);
	} else {
		plan = layoutContainerWithConstrainedSize((JComponent) parent,
				preferredSize, x, y, width, height);
	}
	plan.install();
}
 
Example 10
Source File: LayerButtonLayout.java    From Pixelitor with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
        int startX = GAP;

        // lay out the checkbox
        Dimension cbSize = checkBox.getPreferredSize();
        checkBox.setBounds(startX, (height - cbSize.height) / 2, cbSize.width, cbSize.height);
        startX += (cbSize.width + GAP - LayerButton.BORDER_WIDTH);

        // lay out the layer icon
        int labelStartY = GAP - LayerButton.BORDER_WIDTH;
        if (isImageLayer) {
            layerLabel.setBounds(startX, labelStartY, labelSize, labelSize);
            startX += labelSize;
        } else {
            layerLabel.setBounds(startX, (height - SMALL_THUMB_SIZE) / 2, SMALL_THUMB_SIZE, SMALL_THUMB_SIZE);
            startX += SMALL_THUMB_SIZE;
        }

        // lay out the mask
        if (maskLabel != null) {
            // no need to add distance between the layer and mask icons
            // because there will be a visual distance due to the borders
            maskLabel.setBounds(startX, labelStartY, labelSize, labelSize);
            startX += (labelSize + GAP);
        } else {
            startX += GAP; // the distance between the layer icon and the text field
        }

        int editorHeight = (int) nameEditor.getPreferredSize().getHeight();

        int remainingWidth = parent.getWidth() - startX;
        int adjustment = 2; // the textfield in Nimbus has two invisible pixels around it
        nameEditor.setBounds(startX - adjustment, (height - editorHeight) / 2, remainingWidth - 3, editorHeight);
    }
}
 
Example 11
Source File: WrappingLayout.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
public void layoutContainer(Container parent) {
    int ncomponents = parent.getComponentCount();

    if (ncomponents == 0) {
        return;
    }

    int w = 0;
    int h = 0;
    for (int i = 0; i < ncomponents; i++) {
        Component comp = parent.getComponent(i);
        Dimension d = PREF_SIZE.getSize(comp, maxButtonWidth);
        if (w < d.width) {
            w = d.width;
        }
        if (h < d.height) {
            h = d.height;
        }
    }

    int parentWidth = parent.getWidth();

    // use up all the width or at least one column
    int columns = Math.max(1, Math.min(ncomponents, parentWidth / w));
    int rows = ncomponents / columns;
    if (ncomponents % columns != 0) {
        rows++;
    }

    int x = 1;
    int currentRow = 0;
    for (int i = 0; i < ncomponents; i++) {
        Component c = parent.getComponent(i);
        int currentWidth = w;
        if (x + currentWidth > parent.getWidth()) {
            x = 1;
            currentRow++;
        }

        // add 1 pixel margin to every component
        //int x = currentColumn * w + 1 + currentColumn;
        int y = currentRow * h + 1 + currentRow;

        c.setBounds(x, y, currentWidth, h);
        x += currentWidth + 1;
    }
}
 
Example 12
Source File: FilterBar.java    From pcgen with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public Dimension preferredLayoutSize(Container target)
{
	synchronized (target.getTreeLock())
	{
		Dimension dim = new Dimension(0, 0);
		int nmembers = target.getComponentCount();

		Insets insets = target.getInsets();
		// Provide a default if the panel has not been displayed yet (i.e. in a dialog)
		int targetWidth = target.getWidth() == 0 ? 400 : target.getWidth();
		int maxwidth = targetWidth - (insets.left + insets.right + getHgap() * 2);
		int width = 0;
		int height = 0;
		int component = 0;
		for (int i = 0; i < nmembers; i++, component++)
		{
			Component m = target.getComponent(i);
			if (m.isVisible())
			{
				Dimension d = m.getPreferredSize();
				if (component > 0)
				{
					if (width + d.width > maxwidth)
					{
						dim.width = Math.max(dim.width, width);
						dim.height += height + getVgap();
						width = 0;
						height = 0;
						component = 0;
					}
					width += getHgap();
				}
				height = Math.max(height, d.height);
				width += d.width;
			}
		}
		dim.width = Math.max(dim.width, width);
		dim.height += height;

		dim.width += insets.left + insets.right + getHgap() * 2;
		dim.height += insets.top + insets.bottom + getVgap() * 2;
		return dim;
	}
}
 
Example 13
Source File: VerticalFlowLayout.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 *  Description of the Method
 *
 *@param  target  Description of Parameter
 */
public void layoutContainer(Container target) {
	synchronized (target.getTreeLock()) {
		Insets insets = target.getInsets();
		int maxheight = target.getHeight() - (insets.top + insets.bottom) - 2 * _vgap;
	    int maxwidth = target.getWidth() - (insets.left + insets.right) - 2 * _hgap;
		int nmembers = target.getComponentCount();

		Dimension preferredSize = preferredLayoutSize(target);
		Dimension targetSize = target.getSize();

		int y = (_valign == TOP)    ? insets.top
			  : (_valign == CENTER) ? (targetSize.height - preferredSize.height) / 2
			  :						  targetSize.height - preferredSize.height - insets.bottom;

		for (int i = 0; i < nmembers; i++) {
			Component m = target.getComponent(i);
			if (m.isVisible()) {
				Dimension d = m.getPreferredSize();
		        if (_hfill ) {
		        	m.setSize(maxwidth, d.height);
		            d.width = maxwidth;
		        }
		        else {
		        	m.setSize(d.width, d.height);
		        }

				if ((y + d.height) <= maxheight) {
					if (y > 0) {
						y += _vgap;
					}

					int x = (_halign == LEFT) ? insets.left
						: (_halign == CENTER) ? (targetSize.width - d.width) / 2
						:						targetSize.width - d.width - insets.right;

					m.setLocation(x + _hgap, y + _vgap);

					y += d.getHeight();

				}
				else {
					break;
				}
			}
		}
	}
}
 
Example 14
Source File: NimbusEditorTabDisplayerUI.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected Rectangle getControlButtonsRectangle( Container parent ) {
    Component c = getControlButtons();
    return new Rectangle( parent.getWidth()-c.getWidth()-4, 2, c.getWidth(), c.getHeight() );
}
 
Example 15
Source File: BufferedCanvasComponent.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
protected boolean canDirectlyAccessGraphics() {
        // TODO: what about popup windows / tooltips???

        // TODO: some of the queries could be cached instead of polling,
        // for example isShowing(), isOpaque(), getParent() etc.

//////        // Shouldn't access graphics - no buffering would cause flickering
//////        if (bufferType == BUFFER_NONE) return false;

        // Cannot access graphics - there are some child components
        if (getComponentCount() != 0) return false;

        // Cannot access graphics - component doesn't fully control its area
        if (!isOpaque()) return false;

        // Cannot access graphics - not in Swing tree
        if (!(getParent() instanceof JComponent)) return false;

        // Cannot access graphics - component not showing, doesn't make sense
        if (!isShowing()) return false;

        // Cannot access graphics - component area is not up-to-date
        Rectangle dirtyRegion = RepaintManager.currentManager(this).
                                getDirtyRegion((JComponent)getParent());
        if (dirtyRegion != null && dirtyRegion.width > 0 &&
            dirtyRegion.height > 0) return false;

        // --- Reused from JViewport -------------------------------------------

        Rectangle clip = new Rectangle(0, 0, getWidth(), getHeight());
        Rectangle oldClip = new Rectangle();
        Rectangle tmp2 = null;
        Container parent;
        Component lastParent = null;
        int x, y, w, h;

        for (parent = this; parent != null && isLightweightComponent(parent); parent = parent.getParent()) {
            x = parent.getX();
            y = parent.getY();
            w = parent.getWidth();
            h = parent.getHeight();

            oldClip.setBounds(clip);
            SwingUtilities.computeIntersection(0, 0, w, h, clip);
            if (!clip.equals(oldClip)) return false;

            if (lastParent != null && parent instanceof JComponent &&
               !((JComponent)parent).isOptimizedDrawingEnabled()) {
                Component comps[] = parent.getComponents();
                int index = 0;

                for (int i = comps.length - 1 ;i >= 0; i--) {
                    if (comps[i] == lastParent) {
                    index = i - 1;
                    break;
                    }
                }

                while (index >= 0) {
                    tmp2 = comps[index].getBounds(tmp2);
                    if (tmp2.intersects(clip)) return false;
                    index--;
                }
            }
            clip.x += x;
            clip.y += y;
            lastParent = parent;
        }

        // No Window parent.
        if (parent == null) return false;

        return true;
    }
 
Example 16
Source File: GtkEditorTabDisplayerUI.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected Rectangle getControlButtonsRectangle( Container parent ) {
    Component c = getControlButtons();
    return new Rectangle( parent.getWidth()-c.getWidth()-4, 4, c.getWidth(), c.getHeight() );
}
 
Example 17
Source File: FilterBar.java    From pcgen with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void layoutContainer(Container target)
{
	synchronized (target.getTreeLock())
	{
		Insets insets = target.getInsets();
		int maxwidth = target.getWidth() - (insets.left + insets.right + getHgap() * 2);
		int nmembers = target.getComponentCount();
		int x = 0;
		int y = insets.top + getVgap();
		int rowh = 0;
		int start = 0;

		boolean ltr = target.getComponentOrientation().isLeftToRight();
		SizeRequirements[] xChildren = new SizeRequirements[nmembers];
		SizeRequirements[] yChildren = new SizeRequirements[nmembers];
		for (int i = 0; i < nmembers; i++)
		{
			Component c = target.getComponent(i);
			if (!c.isVisible())
			{
				xChildren[i] = new SizeRequirements(0, 0, 0, c.getAlignmentX());
				yChildren[i] = new SizeRequirements(0, 0, 0, c.getAlignmentY());
			}
			else
			{
				Dimension min = c.getMinimumSize();
				Dimension typ = c.getPreferredSize();
				Dimension max = c.getMaximumSize();
				xChildren[i] = new SizeRequirements(min.width, typ.width, max.width, c.getAlignmentX());
				yChildren[i] = new SizeRequirements(min.height, typ.height, max.height, c.getAlignmentY());

				if ((x == 0) || ((x + typ.width) <= maxwidth))
				{
					if (x > 0)
					{
						x += getHgap();
					}
					x += typ.width;
					rowh = Math.max(rowh, typ.height);
				}
				else
				{
					layoutComponents(target, insets.left + getHgap(), y, maxwidth, rowh, xChildren, yChildren,
						start, i, ltr);

					x = typ.width;
					y += getVgap() + rowh;
					rowh = typ.height;
					start = i;
				}
			}
		}
		layoutComponents(target, insets.left + getHgap(), y, maxwidth, rowh, xChildren, yChildren, start,
			nmembers, ltr);
	}
}
 
Example 18
Source File: FlatEditorTabDisplayerUI.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected Rectangle getControlButtonsRectangle(Container parent) {
    Component c = getControlButtons();
    return new Rectangle(parent.getWidth() - c.getWidth() - ICON_X_PAD,
            (parent.getHeight() - c.getHeight()) / 2, c.getWidth(), c.getHeight());
}
 
Example 19
Source File: FilterBar.java    From pcgen with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public Dimension minimumLayoutSize(Container target)
{
	synchronized (target.getTreeLock())
	{
		Dimension dim = new Dimension(0, 0);
		int nmembers = target.getComponentCount();

		Insets insets = target.getInsets();
		int maxwidth = target.getWidth() - (insets.left + insets.right + getHgap() * 2);
		int width = 0;
		int height = 0;
		int component = 0;
		for (int i = 0; i < nmembers; i++, component++)
		{
			Component m = target.getComponent(i);
			if (m.isVisible())
			{
				Dimension d = m.getMinimumSize();
				if (component > 0)
				{
					if (width + d.width > maxwidth)
					{
						dim.width = Math.max(dim.width, width);
						dim.height += height + getVgap();
						width = 0;
						height = 0;
						component = 0;
					}
					width += getHgap();
				}
				height = Math.max(height, d.height);
				width += d.width;
			}
		}
		dim.width = Math.max(dim.width, width);
		dim.height += height;

		dim.width += insets.left + insets.right + getHgap() * 2;
		dim.height += insets.top + insets.bottom + getVgap() * 2;
		return dim;
	}
}