views:

353

answers:

1

I have a JTable inside of a JScrollPane. 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. I've done this by using a Popup to show a new JScrollPane containing the JList.

Everything is working, except for the position of the 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. Even if I pass in owner the JScrollPane that they are relative to, it doesn't work. I get the same problem if I use text.getX() / text.getY().

How can I position my Popup directly below the 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. However, just plain getLocationOnScreen().y DOES work. Problem solved, thank you.

+1  A: 

You can query the absolute screen position with Component.getLocationOnScreen(). Is that what you're looking for?

Michael Myers