Java tutorial
//package com.java2s; /* * Copyright (C) 2011 Peransin Nicolas. All rights reserved. * Use is subject to license terms. */ import java.awt.Component; import java.awt.Container; import javax.swing.JRootPane; import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.JViewport; import javax.swing.RootPaneContainer; public class Main { public static final String VIEW_SUFFIX = "?view"; public static Component getChild(Component parent, String name) { parent = getContainer(parent); if (parent instanceof JSplitPane) { JSplitPane split = (JSplitPane) parent; if (JSplitPane.TOP.equals(name)) { return split.getTopComponent(); } else if (JSplitPane.LEFT.equals(name)) { return split.getLeftComponent(); } else if (JSplitPane.RIGHT.equals(name)) { return split.getRightComponent(); } else if (JSplitPane.BOTTOM.equals(name)) { return split.getBottomComponent(); } } Container cont = (Container) parent; for (int i = 0; i < cont.getComponentCount(); i++) { Component comp = cont.getComponent(i); if (name.equals(comp.getName())) { return comp; } } if (name.endsWith(VIEW_SUFFIX)) { String subName = name.substring(0, name.length() - VIEW_SUFFIX.length()); if (subName.isEmpty()) { return parent; } return getContainer(getChild(parent, subName)); } throw new IllegalArgumentException("No component named " + name); } private static Component getContainer(Component parent) { while (true) { if (parent instanceof RootPaneContainer) { parent = ((RootPaneContainer) parent).getRootPane(); } else if (parent instanceof JRootPane) { parent = ((JRootPane) parent).getContentPane(); } else if (parent instanceof JScrollPane) { parent = ((JScrollPane) parent).getViewport(); } else if (parent instanceof JViewport) { parent = ((JViewport) parent).getView(); } else { return parent; } } } }