Swing: positioning a popup from a JScrollPane
I have it JTable
inside JScrollPane
. I am creating a custom cell editor for one of the table columns and I want this editor to pop up by scrolling JList
. I did this using Popup
to show the new JScrollPane
containing JList
.
Everything works except for the position Popup
. My custom editor component 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();
}
}
It happens that getX()
they getY()
return the position of the table cell relative to the JScrollPane, holding it, and Popup
- the absolute position of the screen. Even if I pass owner
JScrollPane
which one they refer to, it doesn't work. I get the same problem if I use text.getX()
/ text.getY()
.
How can I place mine Popup
directly underneath TextBox
?
Slightly more background: The end goal is a multiple choice combo selection that displays all selected items in a comma-separated list. If something else similar already exists, please feel free to point me to it.
Edit: owner.getLocationOnScreen().y + getY()
Doesn't work when the scrollbar is anywhere but scrolls to the top. However, it just works getLocationOnScreen().y
. Problem solved, thanks.
a source to share
You can query the absolute screen position with Component.getLocationOnScreen()
. Is this what you are looking for?
a source to share