Difference between validate(), revalidate() and invalidate() in Swing GUI

invalidate() marks the container as invalid. Means the content is somehow wrong and must be re-laid out. But it’s just a kind of mark/flag. It’s possible that multiple invalid containers must be refreshed later.

validate() performs relayout. It means invalid content is asked for all the sizes and all the subcomponents’ sizes are set to proper values by LayoutManager.

revalidate() is just sum of both. It marks the container as invalid and performs layout of the container.

UPDATE:

Some code from Component.java

public void revalidate() {
    revalidateSynchronously();
}

/**
 * Revalidates the component synchronously.
 */
final void revalidateSynchronously() {
    synchronized (getTreeLock()) {
        invalidate();

        Container root = getContainer();
        if (root == null) {
            // There's no parents. Just validate itself.
            validate();
        } else {
            while (!root.isValidateRoot()) {
                if (root.getContainer() == null) {
                    // If there's no validate roots, we'll validate the
                    // topmost container
                    break;
                }

                root = root.getContainer();
            }

            root.validate();
        }
    }
}

Leave a Comment