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

The following examples show how to use java.awt.Container#getComponent() . 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: XComponentPeer.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void addTree(Collection order, Set set, Container cont) {
    for (int i = 0; i < cont.getComponentCount(); i++) {
        Component comp = cont.getComponent(i);
        ComponentPeer peer = comp.getPeer();
        if (peer instanceof XComponentPeer) {
            Long window = Long.valueOf(((XComponentPeer)peer).getWindow());
            if (!set.contains(window)) {
                set.add(window);
                order.add(window);
            }
        } else if (comp instanceof Container) {
            // It is lightweight container, it might contain heavyweight components attached to this
            // peer
            addTree(order, set, (Container)comp);
        }
    }
}
 
Example 2
Source File: DialogLayout.java    From opt4j with MIT License 6 votes vote down vote up
@Override
public void layoutContainer(Container parent) {

	int divider = getDivider(parent);
	Insets insets = parent.getInsets();

	int w = parent.getWidth() - insets.left - insets.right - divider;
	int x = insets.left;
	int y = insets.top;
	for (int k = 1; k < parent.getComponentCount(); k += 2) {
		Component comp1 = parent.getComponent(k - 1);
		Component comp2 = parent.getComponent(k);
		Dimension d = comp2.getPreferredSize();
		comp1.setBounds(x, y, divider - hGap, d.height);
		comp2.setBounds(x + divider, y, w, d.height);
		y += d.height + vGap;
	}

}
 
Example 3
Source File: VerticalFlowLayout.java    From Spark with Apache License 2.0 6 votes vote down vote up
/**
    * Returns the minimum size needed to layout the target container.
    *
    * @param target the component to lay out.
    * @return the minimum layout dimension.
    */
   @Override
public Dimension minimumLayoutSize(Container target) {
       Dimension tarsiz = new Dimension(0, 0);

       for (int i = 0; i < target.getComponentCount(); i++) {
           Component m = target.getComponent(i);
           if (m.isVisible()) {
               Dimension d = m.getMinimumSize();
               tarsiz.width = Math.max(tarsiz.width, d.width);
               if (i > 0) {
                   tarsiz.height += vgap;
               }
               tarsiz.height += d.height;
           }
       }
       Insets insets = target.getInsets();
       tarsiz.width += insets.left + insets.right + hgap * 2;
       tarsiz.height += insets.top + insets.bottom + vgap * 2;
       return tarsiz;
   }
 
Example 4
Source File: XComponentPeer.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private void addTree(Collection order, Set set, Container cont) {
    for (int i = 0; i < cont.getComponentCount(); i++) {
        Component comp = cont.getComponent(i);
        ComponentPeer peer = comp.getPeer();
        if (peer instanceof XComponentPeer) {
            Long window = Long.valueOf(((XComponentPeer)peer).getWindow());
            if (!set.contains(window)) {
                set.add(window);
                order.add(window);
            }
        } else if (comp instanceof Container) {
            // It is lightweight container, it might contain heavyweight components attached to this
            // peer
            addTree(order, set, (Container)comp);
        }
    }
}
 
Example 5
Source File: JDesktopPane.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static Collection<JInternalFrame> getAllFrames(Container parent) {
    int i, count;
    Collection<JInternalFrame> results = new LinkedHashSet<>();
    count = parent.getComponentCount();
    for (i = 0; i < count; i++) {
        Component next = parent.getComponent(i);
        if (next instanceof JInternalFrame) {
            results.add((JInternalFrame) next);
        } else if (next instanceof JInternalFrame.JDesktopIcon) {
            JInternalFrame tmp = ((JInternalFrame.JDesktopIcon) next).getInternalFrame();
            if (tmp != null) {
                results.add(tmp);
            }
        } else if (next instanceof Container) {
            results.addAll(getAllFrames((Container) next));
        }
    }
    return results;
}
 
Example 6
Source File: VerticalFlowLayout.java    From Spark with Apache License 2.0 6 votes vote down vote up
/**
 * places the components defined by first to last within the target
 * container using the bounds box defined.
 *
 * @param target the container.
 * @param x      the x coordinate of the area.
 * @param y      the y coordinate of the area.
 * @param width  the width of the area.
 * @param height the height of the area.
 * @param first  the first component of the container to place.
 * @param last   the last component of the container to place.
 */
private void placethem(Container target, int x, int y, int width, int height,
                       int first, int last) {
    int align = getAlignment();
    if (align == MIDDLE)
        y += height / 2;
    if (align == BOTTOM)
        y += height;

    for (int i = first; i < last; i++) {
        Component m = target.getComponent(i);
        Dimension md = m.getSize();
        if (m.isVisible()) {
            int px = x + (width - md.width) / 2;
            m.setLocation(px, y);
            y += vgap + md.height;
        }
    }
}
 
Example 7
Source File: MFlowLayout.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Dimension preferredLayoutSize(Container target) {
  synchronized (target.getTreeLock()) {
    Dimension dim = new Dimension(0, 0);

    for (int i = 0; i < target.getComponentCount(); i++) {
      Component m = target.getComponent(i);
      if (m.isVisible()) {
        Dimension d = m.getPreferredSize();

        // original
        // dim.height = Math.max(dim.height, d.height);
        // if (i > 0) { dim.width += hgap; }
        // dim.width += d.width;

        // new way
        Point p = m.getLocation();
        dim.width = Math.max(dim.width, p.x + d.width);
        dim.height = Math.max(dim.height, p.y + d.height);
      }
    }
    Insets insets = target.getInsets();
    dim.width += insets.left + insets.right + getHgap() * 2;
    dim.height += insets.top + insets.bottom + getVgap() * 2;
    return dim;
  }
}
 
Example 8
Source File: CompactLayout.java    From openjdk-jdk9 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 9
Source File: SpringLayout.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public void layoutContainer(Container parent) {
    setParent(parent);

    int n = parent.getComponentCount();
    getConstraints(parent).reset();
    for (int i = 0 ; i < n ; i++) {
        getConstraints(parent.getComponent(i)).reset();
    }

    Insets insets = parent.getInsets();
    Constraints pc = getConstraints(parent);
    abandonCycles(pc.getX()).setValue(0);
    abandonCycles(pc.getY()).setValue(0);
    abandonCycles(pc.getWidth()).setValue(parent.getWidth() -
                                          insets.left - insets.right);
    abandonCycles(pc.getHeight()).setValue(parent.getHeight() -
                                           insets.top - insets.bottom);

    for (int i = 0 ; i < n ; i++) {
        Component c = parent.getComponent(i);
        Constraints cc = getConstraints(c);
        int x = abandonCycles(cc.getX()).getValue();
        int y = abandonCycles(cc.getY()).getValue();
        int width = abandonCycles(cc.getWidth()).getValue();
        int height = abandonCycles(cc.getHeight()).getValue();
        c.setBounds(insets.left + x, insets.top + y, width, height);
    }
}
 
Example 10
Source File: SpringLayout.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void layoutContainer(Container parent) {
    setParent(parent);

    int n = parent.getComponentCount();
    getConstraints(parent).reset();
    for (int i = 0 ; i < n ; i++) {
        getConstraints(parent.getComponent(i)).reset();
    }

    Insets insets = parent.getInsets();
    Constraints pc = getConstraints(parent);
    abandonCycles(pc.getX()).setValue(0);
    abandonCycles(pc.getY()).setValue(0);
    abandonCycles(pc.getWidth()).setValue(parent.getWidth() -
                                          insets.left - insets.right);
    abandonCycles(pc.getHeight()).setValue(parent.getHeight() -
                                           insets.top - insets.bottom);

    for (int i = 0 ; i < n ; i++) {
        Component c = parent.getComponent(i);
        Constraints cc = getConstraints(c);
        int x = abandonCycles(cc.getX()).getValue();
        int y = abandonCycles(cc.getY()).getValue();
        int width = abandonCycles(cc.getWidth()).getValue();
        int height = abandonCycles(cc.getHeight()).getValue();
        c.setBounds(insets.left + x, insets.top + y, width, height);
    }
}
 
Example 11
Source File: BaseDialog.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
private void commitTableEditors(Container con) {
    for (int i = 0; i < con.getComponentCount(); i++) {
        if (con.getComponent(i) instanceof Container)
            commitTableEditors((Container) con.getComponent(i));
        final Component container = con.getComponent(i);
        commitComponent(container);
        if (container instanceof JScrollPane) {
            commitComponent(((JScrollPane) container).getViewport()
                    .getView());
        }

    }
}
 
Example 12
Source File: PanelSupportedFrameworksVisual.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void enableComponents(Container root, boolean enabled) {
    root.setEnabled(enabled);
    for (int i = 0; i < root.getComponentCount(); i++) {
        Component child = root.getComponent(i);
        if (child instanceof Container) {
            enableComponents((Container)child, enabled);
        } else {
            child.setEnabled(enabled);
        }
    }
}
 
Example 13
Source File: ENHGridLayout.java    From tn5250j with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Traverses the children and determines row heights and column widths.
 * @param parent the component which needs to be laid out
 * @param min if true, the minimum size is used. Otherwise, the preferred size
 * is used.
 */
protected void getGridSizes(Container parent, boolean min) {
   int ncomponents = parent.getComponentCount();
   if (ncomponents == 0) return;
   int nrows = rows, ncols = cols;
   if (nrows > 0)
       ncols = (ncomponents + nrows - 1) / nrows;
   else
       nrows = (ncomponents + ncols - 1) / ncols;

   row_heights = new int[nrows];
   col_widths = new int[ncols];

   for (int i = 0; i < ncomponents; i++) {
       Component comp = parent.getComponent(i);
      Dimension d = min ? comp.getMinimumSize() :
                   comp.getPreferredSize();

      int row = i / ncols;
      if (d.height > row_heights[row])
         row_heights[row] = d.height;

      int col = i % ncols;
      if (d.width > col_widths[col])
         col_widths[col] = d.width;
   }
}
 
Example 14
Source File: BaseDialog.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
private void setEditsAction(final Container con) {
      if (con instanceof JSpinner)
          return;

/*
       * if(con instanceof JSplitPane){
 * setEditsAction(((JSplitPane)con).getLeftComponent());
 * setEditsAction(((JSplitPane)con).getRightComponent()); return; }
 */

      for (int i = 0; i < con.getComponentCount(); i++) {
          if (con.getComponent(i) instanceof Container)
              setEditsAction((Container) con.getComponent(i));
          final Component container = con.getComponent(i);
          if (container instanceof JTextField) {
              processTextField((JTextField) container);
          } else if (container instanceof JPasswordField) {
              ((JPasswordField) container).addActionListener(listener);
          }
          if (container instanceof JTextComponent) {
              addUndoFunctions((JTextComponent) container);
          }
          if (container instanceof JList
                  && !(container instanceof JTableHeader)) {
              ((JList) container).addMouseListener(new MouseAdapter() {

                  @Override
                  public void mouseClicked(final MouseEvent e) {
                      if (e.getButton() == MouseEvent.BUTTON1
                              && e.getClickCount() > 1)
                          onOk();
                  }

              });
          }
      }
  }
 
Example 15
Source File: VerticalFlowLayout.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  Description of the Method
 *
 *@param  target  Description of Parameter
 *@return         Description of the Returned Value
 */
public Dimension minimumLayoutSize(Container target) {
	synchronized (target.getTreeLock()) {
		Dimension dim = new Dimension(0, 0);
		int nmembers = target.getComponentCount();
		boolean firstVisibleComponent = true;

		for (int ii = 0; ii < nmembers; ii++) {
			Component m = target.getComponent(ii);
			if (m.isVisible()) {
				Dimension d = m.getPreferredSize();
				dim.width = Math.max(dim.width, d.width);
				if (firstVisibleComponent) {
					firstVisibleComponent = false;
				}
				else {
					dim.height += _vgap;
				}
				dim.height += d.height;
			}
		}
		Insets insets = target.getInsets();
		dim.width += insets.left + insets.right + _hgap * 2;
		dim.height += insets.top + insets.bottom + _vgap * 2;
		return dim;
	}
}
 
Example 16
Source File: WrapLayout.java    From Briss-2.0 with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns the minimum or preferred dimension needed to layout the target
 * container.
 *
 * @param target    target to get layout size for
 * @param preferred should preferred size be calculated
 * @return the dimension to layout the target container
 */
private Dimension layoutSize(final Container target, final boolean preferred) {
    synchronized (target.getTreeLock()) {
        // Each row must fit with the width allocated to the containter.
        // When the container width = 0, the preferred width of the
        // container
        // has not yet been calculated so lets ask for the maximum.

        int targetWidth = target.getSize().width;

        if (targetWidth == 0) {
            targetWidth = Integer.MAX_VALUE;
        }

        int hgap = getHgap();
        int vgap = getVgap();
        Insets insets = target.getInsets();
        int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
        int maxWidth = targetWidth - horizontalInsetsAndGap;

        // Fit components into the allowed width

        Dimension dim = new Dimension(0, 0);
        int rowWidth = 0;
        int rowHeight = 0;

        int nmembers = target.getComponentCount();

        for (int i = 0; i < nmembers; i++) {
            Component m = target.getComponent(i);

            if (m.isVisible()) {
                Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();

                // Can't add the component to current row. Start a new row.

                if (rowWidth + d.width > maxWidth) {
                    addRow(dim, rowWidth, rowHeight);
                    rowWidth = 0;
                    rowHeight = 0;
                }

                // Add a horizontal gap for all components after the first

                if (rowWidth != 0) {
                    rowWidth += hgap;
                }

                rowWidth += d.width;
                rowHeight = Math.max(rowHeight, d.height);
            }
        }

        addRow(dim, rowWidth, rowHeight);

        dim.width += horizontalInsetsAndGap;
        dim.height += insets.top + insets.bottom + vgap * 2;

        // When using a scroll pane or the DecoratedLookAndFeel we need to
        // make sure the preferred size is less than the size of the
        // target containter so shrinking the container size works
        // correctly. Removing the horizontal gap is an easy way to do this.

        Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);

        if (scrollPane != null) {
            dim.width -= (hgap + 1);
        }

        return dim;
    }
}
 
Example 17
Source File: Test6524757.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static Component getC(Component component, int index) {
    Container container = (Container) component;
    return container.getComponent(index);
}
 
Example 18
Source File: MiscUtil.java    From energy2d with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static SpringLayout.Constraints getConstraintsForCell(int r, int c, Container parent, int cols) {
	SpringLayout layout = (SpringLayout) parent.getLayout();
	Component component = parent.getComponent(r * cols + c);
	return layout.getConstraints(component);
}
 
Example 19
Source File: WrapLayout.java    From jpexs-decompiler with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns the minimum or preferred dimension needed to layout the target
 * container.
 *
 * @param target target to get layout size for
 * @param preferred should preferred size be calculated
 * @return the dimension to layout the target container
 */
private Dimension layoutSize(Container target, boolean preferred) {
    synchronized (target.getTreeLock()) {
        //  Each row must fit with the width allocated to the containter.
        //  When the container width = 0, the preferred width of the container
        //  has not yet been calculated so lets ask for the maximum.

        int targetWidth = target.getSize().width;

        if (targetWidth == 0) {
            targetWidth = Integer.MAX_VALUE;
        }

        int hgap = getHgap();
        int vgap = getVgap();
        Insets insets = target.getInsets();
        int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
        int maxWidth = targetWidth - horizontalInsetsAndGap;

        //  Fit components into the allowed width
        Dimension dim = new Dimension(0, 0);
        int rowWidth = 0;
        int rowHeight = 0;

        int nmembers = target.getComponentCount();

        for (int i = 0; i < nmembers; i++) {
            Component m = target.getComponent(i);

            if (m.isVisible()) {
                Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();

                //  Can't add the component to current row. Start a new row.
                if (rowWidth + d.width > maxWidth) {
                    addRow(dim, rowWidth, rowHeight);
                    rowWidth = 0;
                    rowHeight = 0;
                }

                //  Add a horizontal gap for all components after the first
                if (rowWidth != 0) {
                    rowWidth += hgap;
                }

                rowWidth += d.width;
                rowHeight = Math.max(rowHeight, d.height);
            }
        }

        addRow(dim, rowWidth, rowHeight);

        dim.width += horizontalInsetsAndGap;
        dim.height += insets.top + insets.bottom + vgap * 2;

        //	When using a scroll pane or the DecoratedLookAndFeel we need to
        //  make sure the preferred size is less than the size of the
        //  target containter so shrinking the container size works
        //  correctly. Removing the horizontal gap is an easy way to do this.
        Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);

        if (scrollPane != null && target.isValid()) {
            dim.width -= (hgap + 1);
        }

        return dim;
    }
}
 
Example 20
Source File: RotatedPanel.java    From pumpernickel with MIT License 4 votes vote down vote up
@Override
public Dimension preferredLayoutSize(Container parent) {
	Component child = parent.getComponent(0);
	Dimension d = child.getPreferredSize();
	return getRotation().transform(d);
}