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
you have to look for SwingUtilities#getDeepestComponentAt, example