Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6815707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:50:21+00:00 2026-05-26T20:50:21+00:00

I am creating a map editor, and where they click will be used to

  • 0

I am creating a map editor, and where they click will be used to add a data point to the map.

    public MapEditor() throws HeadlessException, FileNotFoundException, XMLStreamException {
    super("MapEditor");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Set JFrame properties.
    this.setTitle("Map Editor");
    this.setSize(PREFERRED_WIDTH, PREFERRED_HEIGHT);
    this.setBackground(Color.gray);

    this.setJMenuBar(makeMenuBar());

    JPanel mainPanel = new JPanel( new BorderLayout());

    Icon image = new ImageIcon("map.jpg");
    JLabel label = new JLabel(image);
    scrollPane = new JScrollPane();
    scrollPane.getViewport().add(label);

    scrollPane.addMouseListener(this);

    mainPanel.add(scrollPane, BorderLayout.CENTER);


    this.getContentPane().add(mainPanel);

    this.getContentPane().add(makeStatusBar(), BorderLayout.SOUTH);

    setVisible(true);
}

I also have the following event for when the mouse is clicked:

public void mouseClicked(MouseEvent e) {
    int x = getX();
    int y = getY();
    System.out.println("clicked at (" + x + ", " + y + ")");
}

However, no matter where I click in the window, it returns the same value. I have noticed that if I move the entire window to somewhere else on my screen that it returns different values. They appear to correspond to the top left corner of the window. I’ve tried adding the MouseListener to different components, but I get the same result with each of them. Some help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:50:21+00:00Added an answer on May 26, 2026 at 8:50 pm

    Use MouseAdapter instead as it is an abstract adapter class for receiving mouse events.

    See the accepted answer of Java MouseListener for code example.

    EDIT: You are not using the MouseEvent variable reference so getX() and getY() will not return what you expect, unless getX() and getY() are your own methods?

    Change your code to:

    public void mouseClicked(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        System.out.println("clicked at (" + x + ", " + y + ")");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a map editor for a little game project that I'm doing. Considering
hi i am creating an map application , initially when i click on the
I'm creating an experimental game (which would eventually become a map editor for a
I'm creating a map editor that saves maps for something. Currently, I've been saving
I am creating a map using the new(ish) v3 of the Google Maps API
Creating a google map with store locations within 50 miles of user entered address.
How would I go about creating a Google map that allows the user to
Creating a patch is very easy in SubVersion, With Tortoise, you right-click and select
Creating an XPathDocument with referenced DTD sometimes throws a web exception. Why?
Creating a server-side socket will fail if I'm trying to use the same port

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.