findComponentAt fails with JDialog

Hi. I've been stuck on this for a couple days now- the findComponetAt method of java.awt.Container seems to dislike JDialogs (as well as me). The following:

import java.awt.*;

import java.awt.event.MouseEvent;

import javax.swing.*;

import javax.swing.event.MouseInputAdapter;

publicclass DummyDisplay{

publicstaticvoid main(String[] args){

JFrame frame =new JFrame();

frame.add(new DummyPanel());

frame.pack();

frame.setVisible(true);

JDialog dialog =new JDialog();

dialog.add(new DummyPanel());

dialog.pack();

dialog.setVisible(true);

}

privatestaticclass DummyPanelextends JPanel{

privatestaticfinallong serialVersionUID = 0;

public DummyPanel(){

add(new JLabel("Here's label 1..."), BorderLayout.WEST);

add(new JLabel("Here's label 2..."), BorderLayout.EAST);

addMouseListener(new MouseInputAdapter(){

publicvoid mousePressed(MouseEvent event){

Point loc = event.getPoint();

Component label = DummyPanel.this.findComponentAt(loc.x, loc.y);

System.out.println("Detected: " + label);

}

});

}

}

}

Displays two windows (a JFrame and JDialog). When I click within the JFrame it detects the labels. However, when I click within the JDialog it finds null.

The getComponentAt method behaves properly in this case, but has a nasty habit of occasionally returning invisible components (depending on when the visibility was set). The issue is with version 1.5.0_08 on Ubuntu (Edgy). Any help would be appreciated! -Damian

[2984 byte] By [atagara] at [2007-11-27 10:09:58]
# 1
use this method and check it getComponentAt
dayanandabva at 2007-7-13 0:46:18 > top of Java-index,Desktop,Core GUI APIs...
# 2
As I mentioned already getComponentAt has different functionality (it may return components that aren't visible, and also only searches the immediate children).
atagara at 2007-7-13 0:46:18 > top of Java-index,Desktop,Core GUI APIs...
# 3

Maybe if you passed the parent frame into the constructor of your dialog like so: -

JFrame frame = new JFrame();

JDialog dialog = new JDialog(frame);

... then as you have a proper heirarchy the getComponentAt may be able to find the dialog rather then returning null.

But I could be wrong as I haven't had time to test it

c0demonk3ya at 2007-7-13 0:46:18 > top of Java-index,Desktop,Core GUI APIs...