如何获取 JComboBox 项目的屏幕坐标

How to get JComboBox item's screen coordinates

我正在尝试获取 JComboBox 中项目的屏幕坐标 (x,y)。最好我想要整个边界框(x、y、宽度、高度),但主要是位置。

此信息有很多用途。单击自动化、检查列表可能遮盖的内容(或列表可能遮盖的内容)、测试显示边缘或其他可能不需要的位置的接近度等。

我只是通过使用 Component.getLocationOnScreen 获取法线组件 (Component) 的坐标。有时我想要中心点,为此我取 getLocationOnScreen 的 x/y 并添加 getWidthgetHeight.

的一半

如何为组合框下 drop-down 项的视觉空间做类似的事情?

例如上图中,我想获取"Test 1"(在drop-down中,不在编辑器中)或[=36=的x,y坐标] 或 "Test 3".

也就是说,如果"Test 1"在(10,10),"Test 2"在(10,20),"Test 3"在(10,30),并且每个都有( (30,10) 的宽度,高度),我想要 f(0) = (10,10)f(1) = (10,20)f(2) = (10,30)。或者,如果我们确实也获得了完整的边界,那么 f(2) = (10,20,30,10),但是同样,完整的边界至少比获得位置是次要的。

How can I do something similar for the visual real estate of the drop-down items under the combo box?

您可以访问用于显示组合框项目的JList。

JComboBox comboBox = new JComboBox(...);
BasicComboPopup popup = (BasicComboPopup)comboBox.getAccessibleContext().getAccessibleChild(0);
JList list = popup.getList();
Rectangle bounds = list.getCellBounds(...);
// bounds will be relative to the bottom of the combo box.

我只在监听器中使用过类似上述的逻辑,所以我不确定弹窗是在第一次创建组合框时创建的,还是在第一次显示弹窗时创建的。