get Viewport Ancestor from Component - Java Swing

Java examples for Swing:JComponent

Description

get Viewport Ancestor from Component

Demo Code


//package com.java2s;
import javax.swing.*;
import java.awt.*;

public class Main {
    public static JViewport getViewportAncestor(Component c) {
        for (Container p = c.getParent(); p != null; p = p.getParent()) {
            if (p instanceof JViewport) {
                return (JViewport) p;
            }//from  w w  w . ja v  a2s .  c o m
        }
        return null;
    }
}

Related Tutorials