当JTable可见时如何触发事件?

时间:2014-09-10 15:45:10

标签: java swing user-interface jtable componentlistener

我想知道JTable是否没有回复ComponentListener。我在JTable的标签中有一个JTabbedPane,我已将ComponentListener实施到JTable,因此我可以在桌子可见时触发。但是,它没有响应!我也注意到它也没有响应JTabbedPane!关于如何查找JTable是否可见的任何想法?

1 个答案:

答案 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);  

  }
}