I have an application with a lot of swing components nested in several containers. I implement a pop-up menu with a right click, showing information based on the context in which the component is located.
To give an example: If I right-click on a JTextField, I want to display "foo" in a popup window if the text field is inside JScrollPane and "bar" if it is not. But JTextField itself can be nested in several other JPanels.
I could do something like this:
public static boolean isInScrollPane(JComponent comp) {
Container c = comp.getParent();
while (c != null) {
if (c instanceof JScrollPane) {
return true;
} else {
c = c.getParent();
}
}
return false;
}
But I'm sure there is a much better solution that is already available, and I just did not find it.
Can someone please give me a hint?
moeTi source
share