cscsaba
cscsaba

Reputation: 1289

Swing ComponentListener.componentShown is not fired

I would like to get the (Graphics g) when the WorldPanel is shown. Thanks to stackoverflow I saw the answer where should I use the getGraphics method (ComponentListener.componentShown), but somehow my listener couldn't catch the componentShown.

What did I wrong ? At the bottom of the code snippet the "hello" can't not be displayed.

public class MainPanel extends javax.swing.JPanel implements ComponentListener {
    private CWorldPanel WorldPanel; // extends JPanel

    private void initGUI() {
        try {
            ...
            this.setLayout(thisLayout);
            {
                WorldPanel = new CWorldPanel();             
                WorldPanel.addComponentListener(this);
...
    @Override
    public void componentShown(ComponentEvent e) {
        System.out.println("hello");

    }

See my explanation and thanks at the bottom of the page (cscsaba)

Upvotes: 1

Views: 973

Answers (1)

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74760

If you want to get the Graphics object in order to paint on your component as soon as it is visible, don't. Simply override the paintComponent method of the object to be shown, and paint in there.

(I have no idea why your ComponentListener does not work, and can't try since your example is incomplete.)

Upvotes: 1

Related Questions