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.

    1 answer 1

    Alternatively, you can add a label to the panel and change its coordinates as the mouse moves. Below I give a couple of options: with the usual label on the other panel (but it is declared in the JFrame class itself), and with the "faceless" label on the first panel. When moving, select this label from the list of components on the first panel, change its text, size and placement. The only problem is if there are more tags on the first panel, then additional checking is needed to avoid taking them.

     import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.BorderLayout; import java.awt.Component; import java.awt.FontMetrics; import javax.swing.JLabel; import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseEvent; public class FrameProba extends JFrame { private JLabel lbStaticLabel; public FrameProba() { super("Frame"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(400, 400); JPanel pnlPanel1 = new JPanel(); pnlPanel1.addMouseMotionListener(new MouseMotionAdapter() { @Override public void mouseMoved(MouseEvent e) { lbStaticLabel.setText("X: " + e.getX() + ", Y: " + e.getY()); JLabel lbMoveLabel = null; for(Component c : pnlPanel1.getComponents()) { if(c.getClass().getSimpleName().equals("JLabel")) lbMoveLabel = (JLabel) c; } if( lbMoveLabel != null) { lbMoveLabel.setText("" + e.getX() + " " + e.getY()); FontMetrics fm = lbMoveLabel.getGraphics().getFontMetrics(); lbMoveLabel.setLocation(e.getX(), e.getY() + 20); // чуть ниже курсора мышки, чтоб было видно lbMoveLabel.setSize(fm.stringWidth(lbMoveLabel.getText()), fm.getHeight()); } } }); getContentPane().add(pnlPanel1, BorderLayout.CENTER); pnlPanel1.setLayout(null); pnlPanel1.add(new JLabel()); // добавляем "безликую" метку JPanel pnlPanel2 = new JPanel(); getContentPane().add(pnlPanel2, BorderLayout.SOUTH); lbStaticLabel = new JLabel(""); pnlPanel2.add(lbStaticLabel); setVisible(true); } public tatic void main(Stsring[] args) { new FrameProba(); } } 
    • 1. Wildly slows down :) 2. Appears do not understand that from different sides of JPanel'i, on which I measure and display the coordinates: screenshot ( vk.cc/6galFF ) - Peter Samokhin
    • It is difficult to understand something on the screen. Where should the coordinates be displayed? And where is this element (label, field ??) declared - in the general frame or class that inherits JPanel , and this class is already on the frame? How are coordinates assigned to the mouseMoved method? - Just So