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 7527063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:00:47+00:00 2026-05-30T04:00:47+00:00

in java i tried using getComponentAt(x, y) method from container class. but for given

  • 0

in java i tried using getComponentAt(x, y) method from container class. but for given (x,y) co ordinates it is always returning me object of class javax.swing.JRootPane even if (x,y) contains some image/JLabel/JButton.

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class temp2 implements MouseListener{
    public static void main(String[] arg){
        frame = new JFrame("adsa");
        frame.setBounds(0, 0, 1000, 1000);
        frame.setVisible(true);
        frame.addMouseListener(new temp2());
        JButton l = new JButton("asdasd");
        frame.add(l);
        l.addMouseListener(new temp2());;
    }
    static JFrame frame;
    @Override
    public void mouseClicked(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
            System.out.println(frame.getComponentAt(x, y).getClass());
        System.out.println(frame.getComponentAt(x, y) instanceof JButton);

    }
    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}

this is the sample code i used for testing..
whenever i click on button i am getting following output:

class javax.swing.JRootPane 
false
  • 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-30T04:00:49+00:00Added an answer on May 30, 2026 at 4:00 am

    you have to look for SwingUtilities#getDeepestComponentAt, example

    import java.awt.Component;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.*;
    
    public class TestMouseListener {
    
        public static void main(String[] args) {
            final JComboBox combo = new JComboBox();
            combo.setEditable(true);
            for (int i = 0; i < 10; i++) {
                combo.addItem(i);
            }
            final JLabel tip = new JLabel();
            tip.setText("Outside combobox");
            JPanel panel = new JPanel();
            panel.add(combo);
            panel.add(tip);
            panel.addMouseListener(new MouseAdapter() {
    
                @Override
                public void mouseEntered(MouseEvent e) {
                    tip.setText("Outside combobox");
                }
    
                @Override
                public void mouseExited(MouseEvent e) {
                    Component c = SwingUtilities.getDeepestComponentAt(
                            e.getComponent(), e.getX(), e.getY());
                    tip.setText(c != null && SwingUtilities.isDescendingFrom(
                            c, combo) ? "Inside combo box" : "Outside combobox");
                }
            });
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.add(panel);
            frame.pack();
            frame.setVisible(true);
    
        }
    
        private TestMouseListener() {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day! I tried using JSTL in java but there's an error: exception javax.servlet.ServletException:
I am using Eclipse to program in Java. I tried installing from this site
How to mask a password from console input? I'm using Java 6. I've tried
I tried using the following query: Query q = getPersistenceManager().newQuery( getPersistenceManager().getExtent(ICommentItem.class, false) ); but
I'm looking to build a tree-like structure for my Java application. I've tried using
I tried writing a program in Java using regex to match a pattern and
Using J2SE, I tried making a LAN application using java.net.*, and it worked perfectly.
I've seen some code such as: out.println(print something); I tried import java.lang.System; but it's
I was using a REGEX pattern in java (given below): for the string: It
I'm currently using Eclipse 3.6 (Helios). I have tried using Ant wsgen task but

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.