我想知道JTable
是否没有回复ComponentListener
。我在JTable
的标签中有一个JTabbedPane
,我已将ComponentListener
实施到JTable
,因此我可以在桌子可见时触发。但是,它没有响应!我也注意到它也没有响应JTabbedPane
!关于如何查找JTable是否可见的任何想法?
答案 0 :(得分:-2)
如何查找JTable是否可见?
来源:Determines if the component is visible in its window at the given screen location.
import java.awt.Component;
import java.awt.Point;
import java.awt.Window;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;
/**
* This class contains a collection of static utility methods for Swing.
*
* @author Heidi Rakels.
*/
public class SwingUtil
{
/**
* <p>
* Determines if the component is visible in its window at the given screen location.
* </p>
*
* @param location A location on the screen.
* @param component A component in a window.
* @return True if the component is visible in its window at the given screen location.
*/
public static boolean locationInComponentVisible(Point location, Component component)
{
// Get the root component in the window.
JRootPane rootPane = getRootPane(component);
if (rootPane != null)
{
Component rootComponent = rootPane.getContentPane();
if (rootComponent != null)
{
// Get the location relative to this root component.
Point locationInRoot = new Point(location);
SwingUtilities.convertPointFromScreen(locationInRoot, rootComponent);
// Get the deepest visible component at the given location.
Component deepestComponent = SwingUtilities.getDeepestComponentAt(rootComponent, locationInRoot.x, locationInRoot.y);
if (deepestComponent != null)
{
boolean result = SwingUtilities.isDescendingFrom(deepestComponent, component);
return result;
}
}
}
return false;
}
/**
* Gets the root pane of the given component.
*
* @param component The component whose root pane is retrieved.
* @return The root pane of the component.
*/
public static JRootPane getRootPane(Component component)
{
if (component instanceof JRootPane) {
return (JRootPane)component;
}
if (component.getParent() != null) {
return getRootPane(component.getParent());
}
// Get the window of the component.
Window window = SwingUtilities.windowForComponent(component);
return getRootPane(window);
}
}