I have a class based on JFrame , it contains an instance of another class, based on JPanel , in which g.create(...) ( Graphics2D ) is created, and I need to show its coordinates near the mouse pointer. Unfortunately, neither the addition of MouseListener on MouseListener itself, nor on JPanel gives this result. Tried to display as follows:
addMouseListener(new MouseAdapter() { @Override public void mouseMoved(MouseEvent e) { super.mouseMoved(e); g.drawString("x: " + e.getX() + ", y: " + e.getY(), e.getX(), e.getY()); repaint(); } }); And so on. Nothing helps, I tried many ways. How can this be implemented? If you can't do this, you would be good to at least change the text in the JLabel , which is in a different class (based on JPanel ), which is placed in the JFrame ... A lot of explanations are coming out, but the point is that if JLabel were right inside the JFrame , in the class code, and I would JFrame coordinates in the JFrame , it would be easy, but when everything is wrapped in and described in different classes in different files, it does not work.