Skip to content

Commit 11c9cfb

Browse files
committed
Use smarter algorithm for calculating relative component location #1349
The relative location between two Swing components can be calculated using `SwingUtilities.convertPoint()`, rather than converting the location of both components to screen coordinates on the WindowBuilder side. Contributes to #1349
1 parent 17bb56e commit 11c9cfb

1 file changed

Lines changed: 3 additions & 12 deletions

File tree

  • org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/utils

org.eclipse.wb.swing/src/org/eclipse/wb/internal/swing/utils/SwingUtils.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2011, 2025 Google, Inc. and others.
2+
* Copyright (c) 2011, 2026 Google, Inc. and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -322,17 +322,8 @@ public static Point getScreenLocation(final Component component) throws Exceptio
322322
*/
323323
public static Point getRelativeLocation(final Component parentComponent, final Component childComponent)
324324
throws Exception {
325-
try {
326-
return runObjectLaterAndWait(() -> {
327-
Point parentLocation = parentComponent.getLocationOnScreen();
328-
Point childLocation = childComponent.getLocationOnScreen();
329-
int relX = childLocation.x - parentLocation.x;
330-
int relY = childLocation.y - parentLocation.y;
331-
return new Point(relX, relY);
332-
});
333-
} catch (IllegalComponentStateException e) {
334-
return new Point();
335-
}
325+
return runObjectLaterAndWait(() -> SwingUtilities.convertPoint(childComponent.getParent(),
326+
childComponent.getLocation(), parentComponent));
336327
}
337328

338329
/**

0 commit comments

Comments
 (0)