Java Utililty Methods JComponent Container

List of utility methods to do JComponent Container

Description

The list of methods to do JComponent Container are organized into topic(s).

Method

WindowgetRootWindow(Component component)
get rootwindow for a component
Component root = SwingUtilities.getRoot(component);
if (root instanceof Window) {
    return (Window) root;
return null;
StringgetStringValue(JComponent component)
get String Value
if (component instanceof JTextComponent) {
    return ((JTextComponent) component).getText();
if (component instanceof JComboBox) {
    return ((JComboBox) component).getSelectedItem().toString();
if (component instanceof JList) {
    return ((JList) component).getSelectedValue().toString();
...
ContainergetTopAncestor(JComponent start)
search of the hierarcy of components for an instance of a given class
Container parent = start.getParent();
if (parent == null)
    return start;
if (parent instanceof JComponent)
    return getTopAncestor((JComponent) parent);
if (parent instanceof JFrame)
    return parent;
if (parent instanceof JDialog)
...
JLayeredPanegetTopLevelContainer(JComponent component)
Returns the layered pane that is the top level container for the given component
Window window = SwingUtilities.windowForComponent(component);
if (window instanceof JWindow) {
    return ((JWindow) window).getLayeredPane();
} else if (window instanceof JFrame) {
    return ((JFrame) window).getLayeredPane();
} else if (window instanceof JDialog) {
    return ((JDialog) window).getLayeredPane();
return null;
WindowgetWindow(final JComponent comp)
get Window
if (comp.getRootPane() != null) {
    final Container cont = comp.getRootPane().getParent();
    if (cont != null) {
        if (cont instanceof Window) {
            return (Window) cont;
return null;
WindowgetWindow(JComponent c)
get Window
Container p = c.getParent();
while (p != null && !(p instanceof Window)) {
    p = p.getParent();
return (Window) p;
PointgetWindowLocation(JComponent start)
search of the hierarcy of components for an instance of a given class
Point loc = start.getLocation();
Container parent = start.getParent();
if (parent == null)
    return loc;
if (parent instanceof JComponent) {
    Point ancestorLoc = getWindowLocation((JComponent) parent);
    return new Point(loc.x + ancestorLoc.x, loc.y + ancestorLoc.y);
} else {
...
voidinvokeRepaintOnLayeredRoot(Component leaf, boolean everything)
If the given component is part of a JLayeredPane the lowest layer of that JLayeredPane is used for the repaint; otherwise the component itself.
Component con = leaf.getParent();
if (con != null && con instanceof JLayeredPane) {
    JLayeredPane lp = (JLayeredPane) con;
    Component[] cs = null;
    synchronized (leaf.getTreeLock()) {
        cs = lp.getComponentsInLayer(lp.lowestLayer());
    if (cs != null && cs.length > 0)
...
booleanisCopyEnabled(JComponent targetComponent)
Testeaza daca o componenta este buna pentru copy
TransferHandler transferHandler = targetComponent.getTransferHandler();
if (transferHandler == null)
    return false;
int sourceActions = transferHandler.getSourceActions(targetComponent);
if (sourceActions == TransferHandler.COPY || sourceActions == TransferHandler.COPY_OR_MOVE)
    return true;
else
    return false;
...
booleanisPasteEnabled(JComponent targetComponent)
Testeaza daca o componenta este buna pentru copy
Clipboard clipboard = getClipboard(targetComponent);
Transferable trans = null;
if (clipboard != null)
    trans = clipboard.getContents(null);
TransferHandler.TransferSupport transferSupport = new TransferHandler.TransferSupport(targetComponent,
        trans);
TransferHandler transferHandler = targetComponent.getTransferHandler();
if (transferHandler != null && trans != null)
...