imogen
imogen

Reputation: 13

When clicking label appear 2 times

I try to do make when we click the frame, it shows the coordinates of the click point. And i did this:

public void mouseClicked(MouseEvent e) {

    int x = e.getX();
    int y = e.getY();
    j1.setLocation(x, y);
    j1.setText("(" + x  + ", " +  y + ")");


}

public void mousePressed(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    j1.setLocation(x, y);
    j1.setText("(" + x  + ", " +  y + ")");



}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}

But when i click it shows the coordinates first at the center then the click point, 2 times. But i just want only one and at the click point. Where did i mistake, what shoul i do?

Upvotes: 1

Views: 50

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285415

Just use mousePressed and that's it. When you click the mouse, 3 methods are often called -- mousePressed, mouseReleased and if the mouse does not move between press and release, mouseClicked. You want to respond to just one of these methods.

So change to:

public void mouseClicked(MouseEvent e) {} // leave this empty

public void mousePressed(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    j1.setLocation(x, y);
    j1.setText("(" + x  + ", " +  y + ")");

    // also be sure to tell your container to re-position components
    revalidate();
    repaint();
}

As a side recommendation, note that rather than having a class implement MouseListener, you could have it extend MouseAdapter, and that way your class wouldn't have to have all those empty MouseListener interface methods.

For example:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class MousePosition extends JPanel {
    private static final int PREF_W = 600;
    private static final int PREF_H = PREF_W;
    // format String for display String
    protected static final String FORMAT = "(%d, %d)";
    private int xPos = -40;
    private int yPos = -40;
    private String displayText = "";

    public MousePosition() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                xPos = e.getX();
                yPos = e.getY();
                // use FORMAT String to create our display text
                displayText = String.format(FORMAT, xPos, yPos);
                repaint();
            }
        });
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawString(displayText, xPos, yPos);

    }

    private static void createAndShowGui() {
        MousePosition mainPanel = new MousePosition();

        JFrame frame = new JFrame("MousePosition");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

Upvotes: 2

Related Questions