Repaints the parent of the given component. - Java Swing

Java examples for Swing:JComponent

Description

Repaints the parent of the given component.

Demo Code


//package com.java2s;

import javax.swing.JComponent;

import javax.swing.SwingUtilities;

public class Main {
    /**// w w w  . ja  v  a2  s  . com
     * Repaints the parent of the given component. If the parent is null, the component itself is repainted.
     * 
     * @param    component      The component whose parent will be repainted.
     */
    public static void repaintParent(JComponent component) {

        // Get the parent of the component.
        JComponent parentComponent = (JComponent) SwingUtilities
                .getAncestorOfClass(JComponent.class, component);

        // Could we find a parent?
        if (parentComponent != null) {
            // Repaint the parent.
            parentComponent.revalidate();
            parentComponent.repaint();
        } else {
            // Repaint the component itself.
            component.revalidate();
            component.repaint();
        }

    }
}

Related Tutorials