0

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);
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
TiagoM
  • 3,458
  • 4
  • 42
  • 83
  • 1
    please do you want to only get Point from Mouse, or redirect this Point to the JPanel in JScrollPane – mKorbel Mar 28 '13 at 12:10
  • Hey ! Thanks for your reply . I want to get the position of the mouse in the JPanel.. But the position that I get using getMousePosition is always of the viewport.. not the entire jlabel.. if there was a method to get the amount of X scrolled and amount of Y scrolled I could do the maths.. – TiagoM Mar 28 '13 at 12:12
  • have look at SwingUtilities.convertXxxXxx, I'm sure that I'm post answer to very similair question(s), please to search in my posts here – mKorbel Mar 28 '13 at 12:14

2 Answers2

1

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

Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
0
evento mouseClicked...

x= event.getX();

luego:

Point pos =    scrollImagen.getViewport().getViewPosition();
pos.translate(x, y);
...
double nuevaPosx = pos.getX();
Cyral
  • 13,999
  • 6
  • 50
  • 90
will
  • 1