javax.swing.JComponent.addAncestorListener()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(10.0k)|赞(0)|评价(0)|浏览(203)

本文整理了Java中javax.swing.JComponent.addAncestorListener()方法的一些代码示例,展示了JComponent.addAncestorListener()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JComponent.addAncestorListener()方法的具体详情如下:
包路径:javax.swing.JComponent
类名称:JComponent
方法名:addAncestorListener

JComponent.addAncestorListener介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

controls.add(username);
JPasswordField password = new JPasswordField();
password.addAncestorListener(new RequestFocusListener(false));
controls.add(password);
p.add(controls, BorderLayout.CENTER);

代码示例来源:origin: de.unkrig.commons/commons-net

private static void
focussify(JComponent component) {
  // This is tricky... see
  //    http://tips4java.wordpress.com/2010/03/14/dialog-focus/
  component.addAncestorListener(new AncestorListener() {
    @Override public void
    ancestorAdded(@Nullable AncestorEvent event) {
      assert event != null;
      JComponent component = event.getComponent();
      component.requestFocusInWindow();
      component.removeAncestorListener(this);
    }
    @Override public void ancestorRemoved(@Nullable AncestorEvent event) {}
    @Override public void ancestorMoved(@Nullable AncestorEvent event)   {}
  });
}

代码示例来源:origin: freeplane/freeplane

public static void focusOn(JComponent component) {
  component.addAncestorListener(new AncestorListener() {
    @Override
    public void ancestorRemoved(AncestorEvent event) {
    }
    @Override
    public void ancestorMoved(AncestorEvent event) {
    }
    @Override
    public void ancestorAdded(AncestorEvent event) {
      final JComponent component = event.getComponent();
      EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
          component.requestFocus();                    }
      });
      component.removeAncestorListener(this);
    }
  });
}

代码示例来源:origin: de.sciss/scisslib

/**
 *    Passes keyboard focus to a given component when
 *    that component is to be presented in a dialog.
 *    This temporarily adds an <code>AncestorListener</code>
 *    to the component, detecting when its parent container
 *    is made visible. This is useful for defining an
 *    initial focus owner in <code>JOptionPane</code> calls for example.
 *
 *    @param    c    the component to make focused once its parent container is shown
 */
public static void setInitialDialogFocus( final JComponent c )
{
  c.addAncestorListener( new AncestorAdapter() {
    public void ancestorAdded( AncestorEvent e ) {
      c.requestFocusInWindow();
      c.removeAncestorListener( this );
    }
  });
}

代码示例来源:origin: stackoverflow.com

public class DateCellEditor extends AbstractCellEditor implements TableCellEditor {

  JDateChooser editor;

  public DateCellEditor() {
    editor = new JDateChooser();
    editor.setLocale(Locale.ENGLISH);
    editor.setDateFormatString("MM/dd/yyyy");
    editor.setFocusable(false); // Key #1

    JComponent editorComponent = (JComponent)editor.getDateEditor();
    editorComponent.addAncestorListener(new AncestorListener() { // Key #2

      @Override
      public void ancestorAdded(AncestorEvent event) {
        ((JComponent)editor.getDateEditor()).requestFocusInWindow();
      }

      @Override
      public void ancestorRemoved(AncestorEvent event) {}

      @Override
      public void ancestorMoved(AncestorEvent event) {}

    });
  }

  ....
}

代码示例来源:origin: raydac/netbeans-mmd-plugin

public Focuser(@Nonnull final JComponent component) {
 this.component = component;
 this.component.addAncestorListener(this);
}

代码示例来源:origin: triplea-game/triplea

public MapUnitTooltipManager(final JComponent parent) {
 checkNotNull(parent);
 this.parent = parent;
 // Timeout to show tooltip is 2 seconds.
 this.timer = new Timer(2000, this);
 this.timer.setRepeats(false);
 // Note: Timer not started yet.
 // Close tooltips when the window becomes inactive - so they don't overlap opened dialogs.
 // Listen to ancestor events as the component may not have a Window yet.
 parent.addAncestorListener(windowDeactivationObserver);
 attachWindowListener();
}

代码示例来源:origin: com.samskivert/samskivert

/**
 * Add a droptarget.
 */
protected void addTarget (
  DropTarget target, JComponent comp, boolean autoremove)
{
  _droppers.put(comp, target);
  addTargetListeners(comp);
  if (autoremove) {
    comp.addAncestorListener(_remover);
  }
}

代码示例来源:origin: org.netbeans.api/org-netbeans-api-visual

/**
 * Creates a view. This method could be called once only. Call the getView method for getting created instance of a view.
 * @return the created view
 */
public JComponent createView () {
  assert component == null;
  component = new SceneComponent (this);
  component.addAncestorListener (new AncestorListener() {
    public void ancestorAdded (AncestorEvent event) {
      repaintSatellite ();
    }
    public void ancestorRemoved (AncestorEvent event) {
      repaintSatellite ();
    }
    public void ancestorMoved (AncestorEvent event) {
    }
  });
  return component;
}

代码示例来源:origin: in.jlibs/org-netbeans-api-visual

/**
 * Creates a view. This method could be called once only. Call the getView method for getting created instance of a view.
 * @return the created view
 */
public JComponent createView () {
  assert component == null;
  component = new SceneComponent (this);
  component.addAncestorListener (new AncestorListener() {
    public void ancestorAdded (AncestorEvent event) {
      repaintSatellite ();
    }
    public void ancestorRemoved (AncestorEvent event) {
      repaintSatellite ();
    }
    public void ancestorMoved (AncestorEvent event) {
    }
  });
  return component;
}

代码示例来源:origin: com.samskivert/samskivert

/**
 * Add a dragsource.
 */
protected void addSource (
  DragSource source, JComponent comp, boolean autoremove)
{
  _draggers.put(comp, source);
  comp.addMouseListener(_sourceListener);
  comp.addMouseMotionListener(_sourceListener);
  if (autoremove) {
    comp.addAncestorListener(_remover);
  }
}

代码示例来源:origin: khuxtable/seaglass

/**
 * Installs a {@link WindowFocusListener} on the given {@link JComponent}'s
 * parent {@link Window}. If the {@code JComponent} doesn't yet have a
 * parent, then the listener will be installed when the component is added
 * to a container.
 *
 * @param component     the component who's parent frame to listen to focus
 *                      changes on.
 * @param focusListener the {@code WindowFocusListener} to notify when focus
 *                      changes.
 */
public static void installWeakWindowFocusListener(JComponent component, WindowFocusListener focusListener) {
  WindowListener   weakFocusListener = createWeakWindowFocusListener(focusListener);
  AncestorListener ancestorListener  = createAncestorListener(component, weakFocusListener);
  component.addAncestorListener(ancestorListener);
}

代码示例来源:origin: de.sciss/scisslib

/**
 *  Adds this adapter to a <code>JComponent</code>.
 *  <strong>Use this method instead of calling
 *  <code>cmp.addAncestorListener(...)</code></strong>
 *  because this method will automatically detect
 *  the component's window. This is crucial for
 *  <code>JRootPane</code> components, because they are already
 *  attached to a window when you register the
 *  listener.
 *
 *  @param  c        the <code>JComponent</code> who will be tracked for
 *                    ancestor changes.
 *  @see    javax.swing.JComponent#addAncestorListener( AncestorListener )
 */
public void addTo( JComponent c )
{
  if( cmp != null ) throw new IllegalStateException( "Already added" );
  cmp = c;
  c.addAncestorListener( this );
  learnWindow( c.getTopLevelAncestor() );
}

代码示例来源:origin: khuxtable/seaglass

/**
 * Installs a listener on the given {@link JComponent}'s parent
 * {@link Window} that repaints the given component when the parent window's
 * focused state changes. If the given component does not have a parent at
 * the time this method is called, then an ancestor listener will be
 * installed that installs a window listener when the components parent
 * changes.
 *
 * @param component the {@code JComponent} to add the repaint focus listener
 *                  to.
 */
public static void installJComponentRepainterOnWindowFocusChanged(JComponent component) {
  // TODO check to see if the component already has an ancestor.
  WindowListener   windowListener   = createWeakWindowFocusListener(createRepaintWindowListener(component));
  AncestorListener ancestorListener = createAncestorListener(component, windowListener);
  component.addAncestorListener(ancestorListener);
}

代码示例来源:origin: org.bidib.jbidib.com.vldocking/vldocking

public void installUI(JComponent c) {
  super.installUI(c);
  DockView v = (DockView) c;
  Container parent = c.getParent();
  if (parent instanceof TabbedDockableContainer) {
    installTabbedDockableBorder(v);
  } else if (parent instanceof SplitContainer) {
    installSingleDockableBorder(v);
  } else {
    installMaximizedDockableBorder(v);
  }
  c.addAncestorListener(ancestorListener);
}

代码示例来源:origin: in.jlibs/org-netbeans-api-visual

public void invokeShow () {
  if (scene.getView () == null)
    return;
  if (shown)
    return;
  shown = true;
  birdView.addMouseMotionListener (this);
  scene.getPriorActions ().addAction (action);
  scene.getView ().addAncestorListener (ancestorListener);
  updateForViewPoint (null);
}

代码示例来源:origin: org.netbeans.api/org-netbeans-api-visual

public void invokeShow () {
  if (scene.getView () == null)
    return;
  if (shown)
    return;
  shown = true;
  birdView.addMouseMotionListener (this);
  scene.getPriorActions ().addAction (action);
  scene.getView ().addAncestorListener (ancestorListener);
  updateForViewPoint (null);
}

代码示例来源:origin: com.google.code.validationframework/validationframework-swing

private void initComponents() {
  toolTip = new JToolTip();
  owner.addComponentListener(locationAdapter);
  owner.addAncestorListener(locationAdapter);
  getRootPane().setWindowDecorationStyle(JRootPane.NONE); // Just in case...
  setFocusable(false); // Just in case...
  setFocusableWindowState(false);
  setContentPane(toolTip);
}

代码示例来源:origin: com.google.code.validationframework/validationframework-swing

/**
 * Attaches the decoration to the specified component.
 *
 * @param componentToBeDecorated Component to be decorated.
 */
private void attach(JComponent componentToBeDecorated) {
  detach();
  decoratedComponent = componentToBeDecorated;
  if (decoratedComponent != null) {
    decoratedComponent.addComponentListener(decoratedComponentTracker);
    decoratedComponent.addAncestorListener(decoratedComponentTracker);
    decoratedComponent.addHierarchyBoundsListener(decoratedComponentTracker);
    decoratedComponent.addHierarchyListener(decoratedComponentTracker);
    decoratedComponent.addPropertyChangeListener("enabled", decoratedComponentTracker);
    decoratedComponent.addPropertyChangeListener("ancestor", decoratedComponentTracker);
    attachToLayeredPane();
  }
}

代码示例来源:origin: com.google.code.validationframework/validationframework-swing

/**
 * Initializes the components of the tooltip window.
 */
private void initComponents() {
  // Avoid warning on Mac OS X when changing the alpha
  getRootPane().putClientProperty("apple.awt.draggableWindowBackground", Boolean.FALSE);
  toolTip = new JToolTip();
  toolTip.addMouseListener(new TransparencyAdapter());
  owner.addComponentListener(locationAdapter);
  owner.addAncestorListener(locationAdapter);
  getRootPane().setWindowDecorationStyle(JRootPane.NONE); // Just in case...
  setFocusable(false); // Just in case...
  setFocusableWindowState(false);
  setContentPane(toolTip);
  pack(); // Seems to help for the very first setVisible(true) when window transparency is on
}

相关文章

JComponent类方法