Question

I didnt know how to make a good title for this.. but here we go:

I am trying to get the accurate mouse position when I click on a label, using a MouseAdapter I added to my the JViewPort of the JScrollPane My panel itself is added inside the JScrollPane.

And when I try to get the coordinates of the mouse position is always relative to the area of the panel that is visible.. its not counting the area that is not visible, because of the scroll..

I dont know If I made myself clear, I hope so.. Already tryed using getMousePosition from JViewPort and from JScrollPane and also did not work.. Thanks alot in advance!!

Here is some code: The construction of the frame that has the panel inside it..

public GraphViewer(ArrayList<TimeSlot> graph) throws HeadlessException {
    final MyCustomPanel panel = new MyCustomPanel(graph);
    panel.setPreferredSize(panel.getLargestSize());

    scroll = new JScrollPane(panel);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    add(scroll, BorderLayout.CENTER);

    JViewport v = scroll.getViewport();
    ViewportDragScrollListener l = new ViewportDragScrollListener(panel);
    v.addMouseMotionListener(l);
    v.addMouseListener(l);
}
Was it helpful?

Solution

What about adding JViewport.getViewPosition() to the coordinates obtained from getMousePosition()?

OTHER TIPS

evento mouseClicked...

x= event.getX();

luego:

Point pos =    scrollImagen.getViewport().getViewPosition();
pos.translate(x, y);
...
double nuevaPosx = pos.getX();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top