Component () { Component focusComponent = myContent; FocusTraversalPolicy policy = myContent.getFocusTraversalPolicy(); if (policy instanceof SortingFocusTraversalPolicy && ((SortingFocusTraversalPolicy)policy).getImplicitDownCycleTraversal()) { focusComponent = policy.getDefaultComponent(myContent); } while (true) { // Setting focus to a JScrollPane is not very useful. Better setting focus to the // contained view. This is useful for Tooltip popups, for example. if (focusComponent instanceof JScrollPane) { JViewport viewport = ((JScrollPane)focusComponent).getViewport(); if (viewport == null) { break; } Component child = viewport.getView(); if (child == null) { break; } focusComponent = child; continue; } // Done if we can't find anything to dive into break; } return focusComponent; }