下面列出了javax.swing.SwingUtilities#getDeepestComponentAt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void updateClientCursor() {
PointerInfo pointerInfo = MouseInfo.getPointerInfo();
if (pointerInfo == null) {
/*
* This can happen when multiple graphics device cannot decide
* which graphics device contains the current mouse position
* or on systems without a mouse
*/
return;
}
Point p = pointerInfo.getLocation();
SwingUtilities.convertPointFromScreen(p, this);
Component target = SwingUtilities.getDeepestComponentAt(this, p.x, p.y);
if (target != null) {
content.setCursor(target.getCursor());
}
}
private void updateClientCursor() {
Point p = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(p, this);
Component target = SwingUtilities.getDeepestComponentAt(this, p.x, p.y);
if (target != null) {
content.setCursor(target.getCursor());
}
}
public int getTabForCoordinate(int x, int y) {
Component c = SwingUtilities.getDeepestComponentAt(tabs, x, y);
while (c != tabs) {
if (c instanceof JComponent) {
JComponent jc = (JComponent) c;
Integer i = (Integer) jc
.getClientProperty(PROPERTY_TAB_INDEX);
if (i != null)
return i;
}
c = c.getParent();
}
return -1;
}
/**
* Get the drop target capable of accepting the dragged entity at the
* current location.
*
* @return drop target, or <code>null</code> if the component below is not a
* DropTarget, or it is not capable of accepting the dragged entity
*/
private DropTarget getCurrentDropTarget() {
if (underlyingComponent == null) {
Container parent = getParent();
Point containerPoint = SwingUtilities.convertPoint(this, point, parent);
underlyingComponent = SwingUtilities.getDeepestComponentAt(parent, containerPoint.x, containerPoint.y);
}
if ((underlyingComponent instanceof DropTarget)
&& (((DropTarget) underlyingComponent).canAccept(dragged.getEntity()))) {
return (DropTarget) underlyingComponent;
}
return null;
}
public static Component getActiveComponent(Component component, int x, int y) {
Component componentx = SwingUtilities.getDeepestComponentAt(component, x, y);
if (componentx == null) {
return component;
}
return componentx;
}
private void updateClientCursor() {
Point p = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(p, this);
Component target = SwingUtilities.getDeepestComponentAt(this, p.x, p.y);
if (target != null) {
content.setCursor(target.getCursor());
}
}
public void showPopup(MouseEvent mouseEvent) {
if (isContextMenuOn()) {
return;
}
Component component = SwingUtilities.getDeepestComponentAt(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY());
if (component == null) {
return;
}
if (component instanceof JMenuItem && (!(component instanceof JMenu) || ((JMenu) component).isSelected())) {
return;
}
Point point = SwingUtilities.convertPoint(mouseEvent.getComponent(), mouseEvent.getPoint(), component);
showPopup(component, point);
}
@Override
public String getToolTipText(MouseEvent event) {
Component c = SwingUtilities.getDeepestComponentAt(this, event.getX(), event.getY());
if (c instanceof JComponent) {
JComponent jc = (JComponent) c;
String tooltip = jc.getToolTipText();
if (null != tooltip) {
return tooltip;
}
}
return super.getToolTipText(event);
}
private void setDispatchComponent(MouseEvent e) {
Component editorComponent = header.getEditorComponent();
Point p = e.getPoint();
Point p2 = SwingUtilities.convertPoint(header, p, editorComponent);
dispatchComponent = SwingUtilities.getDeepestComponentAt(editorComponent,
p2.x, p2.y);
}
private void updateClientCursor() {
Point p = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(p, this);
Component target = SwingUtilities.getDeepestComponentAt(this, p.x, p.y);
if (target != null) {
content.setCursor(target.getCursor());
}
}
private void updateClientCursor() {
Point p = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(p, this);
Component target = SwingUtilities.getDeepestComponentAt(this, p.x, p.y);
if (target != null) {
content.setCursor(target.getCursor());
}
}
private void updateClientCursor() {
Point p = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(p, this);
Component target = SwingUtilities.getDeepestComponentAt(this, p.x, p.y);
if (target != null) {
content.setCursor(target.getCursor());
}
}
private void redispatchMouseEvent(MouseEvent event) {
Point glassPanePoint = event.getPoint();
Point containerPoint = SwingUtilities.convertPoint(mGlassPane, glassPanePoint, mCaptureComponent);
Component component = SwingUtilities.getDeepestComponentAt(mCaptureComponent, containerPoint.x, containerPoint.y);
if (component != null) {
Point componentPoint = SwingUtilities.convertPoint(mGlassPane, glassPanePoint, component);
component.dispatchEvent(new MouseEvent(component, event.getID(), event.getWhen(), event.getModifiersEx(), componentPoint.x, componentPoint.y, event.getClickCount(), event.isPopupTrigger()));
}
}
private void updateClientCursor() {
Point p = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(p, this);
Component target = SwingUtilities.getDeepestComponentAt(this, p.x, p.y);
if (target != null) {
content.setCursor(target.getCursor());
}
}
private void updateClientCursor() {
Point p = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(p, this);
Component target = SwingUtilities.getDeepestComponentAt(this, p.x, p.y);
if (target != null) {
content.setCursor(target.getCursor());
}
}
private void setDispatchComponent(MouseEvent e) {
Component editorComponent = header.getEditorComponent();
Point p = e.getPoint();
Point p2 = SwingUtilities.convertPoint(header, p, editorComponent);
dispatchComponent = SwingUtilities.getDeepestComponentAt(editorComponent, p2.x, p2.y);
}
private boolean redispatchComponent(MouseEvent e) {
Point p = e.getPoint();
int index = list.locationToIndex(p);
if (index < 0 || index >= list.getModel().getSize()) {
return false;
}
ListCellRenderer renderer = list.getCellRenderer();
if (null == renderer) {
return false;
}
Component renComponent = renderer.getListCellRendererComponent(list, list.getModel().getElementAt(index), index, false, false);
if (null == renComponent) {
return false;
}
Rectangle rect = list.getCellBounds(index, index);
if (null == rect) {
return false;
}
renComponent.setBounds(0, 0, rect.width, rect.height);
renComponent.doLayout();
Point p3 = rect.getLocation();
Point p2 = new Point(p.x - p3.x, p.y - p3.y);
Component dispatchComponent =
SwingUtilities.getDeepestComponentAt(renComponent,
p2.x, p2.y);
if ( e.isPopupTrigger() &&
dispatchComponent instanceof LinkButton &&
!((LinkButton)dispatchComponent).isHandlingPopupEvents() )
{
return false;
}
if (dispatchComponent instanceof AbstractButton) {
if (!((AbstractButton) dispatchComponent).isEnabled()) {
return false;
}
Point p4 = SwingUtilities.convertPoint(renComponent, p2, dispatchComponent);
MouseEvent newEvent = new MouseEvent(dispatchComponent,
e.getID(),
e.getWhen(),
e.getModifiers(),
p4.x, p4.y,
e.getClickCount(),
e.isPopupTrigger(),
MouseEvent.NOBUTTON);
dispatchComponent.dispatchEvent(newEvent);
list.repaint(rect);
e.consume();
return true;
}
return false;
}
public static Component findComponentUnderMouse() {
Window window = findWindow();
Point location = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(location, window);
return SwingUtilities.getDeepestComponentAt(window, location.x, location.y);
}
/**
* Clicks the JComponent at the given point from within the given provider.
*
* @param provider The provider to be clicked.
* @param button The mouse button to use (left, center, right)
* @param x the x location of the click
* @param y the y location of the click
* @param clickCount the number of times to click
* @param modifiers the modifiers to apply (Ctrl, Alt, etc; 0 is none)
* @param popupTrigger true if this click should show a popup menu
* @return the actual Java JComponent that was clicked
*/
public static Component clickComponentProvider(ComponentProvider provider, int button, int x,
int y, int clickCount, int modifiers, boolean popupTrigger) {
JComponent component = provider.getComponent();
final Component clickComponent = SwingUtilities.getDeepestComponentAt(component, x, y);
clickMouse(clickComponent, MouseEvent.BUTTON1, x, y, clickCount, modifiers, popupTrigger);
return clickComponent;
}
/**
* Returns <code>true</code> if the given {@link MouseEvent} only exited to child components of
* the specified parent {@link Component}; <code>false</code> otherwise. This fixes the problem
* that Swing triggers MouseExited events when moving the mouse for example over a button inside
* the panel on which the mouse listener is.
*
* @param parent
* @param e
* @return
*/
public static boolean isMouseEventExitedToChildComponents(final Component parent, final MouseEvent e) {
if (parent == null) {
throw new IllegalArgumentException("parent must not be null!");
}
return SwingUtilities.getDeepestComponentAt(parent, e.getX(), e.getY()) != null;
}