下面列出了javax.swing.JComponent#getComponents ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns a set of all components under the given parent.
* @param parent the parent
* @return the set of components
*/
public static Set<JComponent> allComponents(JComponent parent) {
Set<JComponent> result = new HashSet<>();
Deque<JComponent> queue = new LinkedList<>();
queue.add(parent);
while (!queue.isEmpty()) {
JComponent c = queue.removeFirst();
result.add(c);
for (Component c0 : c.getComponents()) {
if (c0 instanceof JComponent) {
queue.add((JComponent)c0);
}
}
}
result.remove(parent);
return result;
}
private static void checkVisibility(JComponent comp) {
if (comp == null) return;
comp.invalidate();
comp.revalidate();
comp.doLayout();
comp.repaint();
for (Component c : comp.getComponents())
if (c.isVisible()) {
comp.setVisible(true);
return;
}
comp.setVisible(false);
}
void started() {
started = true;
((CardLayout) progress.getLayout()).show(progress, "progress");
progress.invalidate();
//disable all elements in the dialog:
List<JComponent> todo = new LinkedList<JComponent>();
todo.add(this);
while (!todo.isEmpty()) {
JComponent c = todo.remove(0);
if (c == progress) continue;
c.setEnabled(false);
for (Component child : c.getComponents()) {
if (child instanceof JComponent) todo.add((JComponent) child);
}
}
}
/**
* Allows to show context popup for all components recursively.
*
* @param component the root component of the tree
* @param value whether or not the popup menu is inherited
*/
private static void setInheritsPopupMenu(JComponent component, boolean value) {
component.setInheritsPopupMenu(value);
for (Object object : component.getComponents()) {
if (object instanceof JComponent) {
setInheritsPopupMenu((JComponent) object, value);
}
}
}
@Override
public int getBaseline(JComponent c, int width, int height) {
Map<Component, Rectangle> blueprint = ControlGridLayout.this
.createBlueprint(c);
for (Component child : c.getComponents()) {
Rectangle r = blueprint.get(child);
if (r != null) {
int y = child.getBaseline(r.width, r.height);
return y + r.y;
}
}
return super.getBaseline(c, width, height);
}
/**
* Allows to show context popup for all components recursively.
*
* @param component the root component of the tree
* @param value whether or not the popup menu is inherited
*/
private static void setInheritsPopupMenu(JComponent component, boolean value) {
component.setInheritsPopupMenu(value);
for (Object object : component.getComponents()) {
if (object instanceof JComponent) {
setInheritsPopupMenu((JComponent) object, value);
}
}
}
/**
* Allows to show context popup for all components recursively.
*
* @param component the root component of the tree
* @param value whether or not the popup menu is inherited
*/
private static void setInheritsPopupMenu(JComponent component, boolean value) {
component.setInheritsPopupMenu(value);
for (Object object : component.getComponents()) {
if (object instanceof JComponent) {
setInheritsPopupMenu((JComponent) object, value);
}
}
}
/**
* Allows to show context popup for all components recursively.
*
* @param component the root component of the tree
* @param value whether or not the popup menu is inherited
*/
private static void setInheritsPopupMenu(JComponent component, boolean value) {
component.setInheritsPopupMenu(value);
for (Object object : component.getComponents()) {
if (object instanceof JComponent) {
setInheritsPopupMenu((JComponent) object, value);
}
}
}
/**
* Allows to show context popup for all components recursively.
*
* @param component the root component of the tree
* @param value whether or not the popup menu is inherited
*/
private static void setInheritsPopupMenu(JComponent component, boolean value) {
component.setInheritsPopupMenu(value);
for (Object object : component.getComponents()) {
if (object instanceof JComponent) {
setInheritsPopupMenu((JComponent) object, value);
}
}
}
@Override
protected void uninstall(JComponent parent) {
super.uninstall(parent);
ContainerListener l = (ContainerListener) parent
.getClientProperty(PROPERTY_CONTAINER_LISTENER);
parent.putClientProperty(PROPERTY_CONTAINER_LISTENER, null);
parent.removeContainerListener(l);
for (Component c : parent.getComponents()) {
((JComponent) c).putClientProperty(
"animatedLayout.propertyListener", null);
((JComponent) c).removePropertyChangeListener(
PROPERTY_DESTINATION, destinationListener);
}
}
/**
* Allows to show context popup for all components recursively.
*
* @param component the root component of the tree
* @param value whether or not the popup menu is inherited
*/
private static void setInheritsPopupMenu(JComponent component, boolean value) {
component.setInheritsPopupMenu(value);
for (Object object : component.getComponents()) {
if (object instanceof JComponent) {
setInheritsPopupMenu((JComponent) object, value);
}
}
}
/**
* Set double buffering for a component and all of its descendants.
*/
private static void setDoubleBuffered(JComponent jc, boolean b) {
jc.setDoubleBuffered(b);
for (Component c : jc.getComponents()) {
if (c instanceof JComponent)
setDoubleBuffered((JComponent) c, b);
}
}
/**
* Allows to show context popup for all components recursively.
*
* @param component the root component of the tree
* @param value whether or not the popup menu is inherited
*/
private static void setInheritsPopupMenu(JComponent component, boolean value) {
component.setInheritsPopupMenu(value);
for (Object object : component.getComponents()) {
if (object instanceof JComponent) {
setInheritsPopupMenu((JComponent) object, value);
}
}
}
/**
* Allows to show context popup for all components recursively.
*
* @param component the root component of the tree
* @param value whether or not the popup menu is inherited
*/
private static void setInheritsPopupMenu(JComponent component, boolean value) {
component.setInheritsPopupMenu(value);
for (Object object : component.getComponents()) {
if (object instanceof JComponent) {
setInheritsPopupMenu((JComponent) object, value);
}
}
}
/**
* Allows to show context popup for all components recursively.
*
* @param component the root component of the tree
* @param value whether or not the popup menu is inherited
*/
private static void setInheritsPopupMenu(JComponent component, boolean value) {
component.setInheritsPopupMenu(value);
for (Object object : component.getComponents()) {
if (object instanceof JComponent) {
setInheritsPopupMenu((JComponent) object, value);
}
}
}
public static void setPanelBackground(JComponent panel, Color color) {
if (panel!=null){
panel.setBackground(color);
for (Component c : panel.getComponents()) {
if (c instanceof JPanel) {
setPanelBackground((JPanel) c, color);
}
}
}
}
/**
* Convenient method to empty all the text fields of a given JComponent.
*
* @param component the component to "blank".
*/
public static void emptyFields (JComponent component)
{
for (Component comp : component.getComponents()) {
if (comp instanceof JTextField) {
((JTextComponent) comp).setText("");
}
}
}
/**
* Allows to show context popup for all components recursively.
*
* @param component the root component of the tree
* @param value whether or not the popup menu is inherited
*/
private static void setInheritsPopupMenu(JComponent component, boolean value) {
component.setInheritsPopupMenu(value);
for (Object object : component.getComponents()) {
if (object instanceof JComponent) {
setInheritsPopupMenu((JComponent) object, value);
}
}
}
/**
* Prepares the component for drawing. This recursively disables the double-buffering as this would interfere with the
* drawing.
*
* @param c
* the component that should be prepared.
*/
private void prepareComponent( final Component c ) {
if ( c instanceof JComponent ) {
final JComponent jc = (JComponent) c;
jc.setDoubleBuffered( false );
final Component[] childs = jc.getComponents();
for ( int i = 0; i < childs.length; i++ ) {
final Component child = childs[i];
prepareComponent( child );
}
}
}
public void installUI(JComponent c) {
super.installUI(c);
//Fetch the "no properties" string - it's not going to change
//for the life of the session
// noPropsString = NbBundle.getMessage(MarginViewportUI.class,
// "CTL_NoProperties"); //NOI18N
//Set an appropriate font and color. Only really relevant on OS-X to
//keep the font consistent with other NB fonts
Color fg = UIManager.getColor("controlShadow"); //NOI18N
if (fg == null) {
fg = Color.LIGHT_GRAY;
}
c.setForeground(fg);
Color bg = UIManager.getColor("Tree.background"); //NOI18N
if (bg == null) {
bg = Color.WHITE;
}
c.setBackground(bg);
Font f = UIManager.getFont("Tree.font"); //NOI18N
if (f == null) {
f = UIManager.getFont("controlFont"); //NOI18N
}
if (f != null) {
c.setFont(f);
}
c.addContainerListener(this);
Component[] kids = c.getComponents();
for (int i = 0; i < kids.length; i++) {
//Should almost always be empty anyway, if not only one component,
//but for completeness...
kids[i].addComponentListener(this);
}
}