ComponentListener?

K

Knute Johnson

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);
}
}
 
S

Stefan Ram

Knute Johnson said:
f.setVisible(true);

Apparently, this does not count. Inserting

t.setVisible( false );
t.setVisible( true );

directly after this seems to trigger the event.
 
K

Knute Johnson

Stefan said:
Apparently, this does not count. Inserting

t.setVisible( false );
t.setVisible( true );

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,
 
C

Christian Kaufhold

Knute Johnson said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,270
Messages
2,571,102
Members
48,773
Latest member
Kaybee

Latest Threads

Top