What am I doing wrong with this ComponentListener? It never calls
componentShown();
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JPanel {
public test() {
setPreferredSize(new Dimension(200,200));
addComponentListener(new ComponentAdapter() {
public void componentShown(ComponentEvent ce) {
System.out.println(ce);
}
});
}
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test t = new test();
f.add(t);
f.pack();
f.setVisible(true);
}
};
EventQueue.invokeLater(r);
}
}

Signature
Knute Johnson
email s/nospam/knute/
Stefan Ram - 18 Nov 2006 00:12 GMT
> f.setVisible(true);
Apparently, this does not count. Inserting
t.setVisible( false );
t.setVisible( true );
directly after this seems to trigger the event.
Knute Johnson - 18 Nov 2006 00:49 GMT
>> f.setVisible(true);
>
[quoted text clipped - 4 lines]
>
> directly after this seems to trigger the event.
Just found an interesting tidbit in the Tutorial:
"The component-hidden and component-shown events occur only as the
result of calls to a Component's setVisible method. For example, a
window might be miniaturized into an icon (iconified) without a
component-hidden event being fired."
It would have been nice if that was in the docs too.
Thanks,

Signature
Knute Johnson
email s/nospam/knute/
Christian Kaufhold - 19 Nov 2006 00:31 GMT
> What am I doing wrong with this ComponentListener? It never calls
> componentShown();
None-Window components are visible by default (but not "showing" until
their Window is made visible). Maybe you are looking
for HierarchyListener.
Christian