简体   繁体   English

摆动:从JScrollPane中定位弹出窗口

[英]Swing: Positioning a popup from within a JScrollPane

I have a JTable inside of a JScrollPane . 我在JScrollPane有一个JTable I am creating a custom cell editor for one of the columns of the table, and I want this editor to pop up a scrolling JList . 我正在为表的某一列创建一个自定义单元格编辑器,并且我希望该编辑器弹出一个滚动的JList I've done this by using a Popup to show a new JScrollPane containing the JList . 我通过使用Popup显示包含JList的新JScrollPane来完成此操作。

Everything is working, except for the position of the Popup . 除了Popup的位置,其他所有东西都在工作。 My custom component for the editor looks basically like this: 我为编辑器定制的组件基本上如下所示:

public class CustomPanel extends JPanel {
    JTextField text = new JTextField();
    JList list = new JList();
    JScrollPane scroll = new JScrollPane(list);
    Component owner = null;
    public CustomPanel(Component owner) {
        this.owner = owner;
        add(text);
    }
    public void showPopup() {
        Popup p = PopupFactory.getPopup(owner, scroll, getX(), getY()+getHeight());
        p.show();
    }
}

What is happening is that getX() and getY() are returning the position of the table cell relative to the JScrollPane holding it, and Popup is wanting absolute screen position. 发生的事情是, getX()getY()返回相对于持有它的JScrollPane的表格单元的位置,而Popup则需要绝对的屏幕位置。 Even if I pass in owner the JScrollPane that they are relative to, it doesn't work. 即使我将owner相对的JScrollPane传递给它,它也不起作用。 I get the same problem if I use text.getX() / text.getY() . 如果使用text.getX() / text.getY()text.getX()同样的问题。

How can I position my Popup directly below the TextBox ? 如何将Popup放置在TextBox正下方?

Just a bit more background: The end goal is a multiple-select combobox that displays all of the selected items as a comma-separated list. 仅需更多背景知识:最终目标是一个多选组合框,该组合框将所有选定项显示为以逗号分隔的列表。 If something else like this already exists, please don't hesitate to point me to it. 如果已经有类似的东西存在,请不要犹豫,将其指向我。

Edit: owner.getLocationOnScreen().y + getY() doesn't work when the scroll pane is anywhere but scrolled all the way up. 编辑: owner.getLocationOnScreen().y + getY()在滚动窗格位于任何地方但一直向上滚动时不起作用。 However, just plain getLocationOnScreen().y DOES work. 但是,仅使用普通的getLocationOnScreen().y Problem solved, thank you. 问题解决了,谢谢。

You can query the absolute screen position with Component.getLocationOnScreen() . 您可以使用Component.getLocationOnScreen()查询屏幕的绝对位置。 Is that what you're looking for? 那是您要找的东西吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM