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

  • Home
  • SEARCH
  • 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 6620687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:08:01+00:00 2026-05-25T21:08:01+00:00

I have a Image displayed inside JLabel using ImageIcon feature. The JLabel is contained

  • 0

I have a Image displayed inside JLabel using ImageIcon feature.

The JLabel is contained inside a JPanel.

When image is clicked, I want to get the mouse position relative to the image.

I am trying to solve this problem by adding mouse listener to the JLabel which contains the
image.

Apparently, even if I set the size of JLabel with the width and height of the image, it seems that JLabel automatically stretches out to the size of JPanel.

So when below code runs, if the size of image is smaller than the JPanel, getting X and Y position returns the unwanted value.

Is there a way that I can specifically limit the size of the JLabel so that getting X position and Y position of the mouse will work fine?

Or better way to get a mouse position relative to the image? (which would be better)

    ImageIcon icon = new ImageIcon(image);

    JLabel imageLabel = new JLabel(icon);
    imageLabel.setSize(image.getWidth(null), image.getHeight(null));

    imageLabel.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {
            JOptionPane.showMessageDialog(imageLabel, e.getX()+" "+e.getY());
        }
    });

    imageLabel.validate();
    panel.add(imageLabel);
    panel.validate();
  • 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-25T21:08:02+00:00Added an answer on May 25, 2026 at 9:08 pm

    It is all about the layout.

    See this example for the above image.


    It would seem this use-case requires a GridBagLayout or BoxLayout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 16 bit grayscale image that I want to display using WPF
Is it possible to have image backgrounds inside HTML e-mails that are displayed across
I have an application that displays an image inside of a Windows Forms PictureBox
I have the following XAML code, which displays a picture (image inside borders) and
I have a image here http://power.itp.ac.cn/~jmyang/funny/fun4.jpg and I want to display it in my
I have image which size was 600 * 600 and it was displayed on
I have articles with display:table and an image wrapper inside with display:table-cell to achieve
I have some bulk images inside DIV tag and If I click an image
I want to make a container of fixed size that will inside have an
I Have a UIScrollview with a UIImageView inside where I scroll from an image

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.