How to catch an event when a JPanel is created ?

Posted on 2005-04-18
Last Modified: 2010-07-27

This is what I do (pseudocode):

MyJPanel j = new MyJPanel()

I need something inside MyJPanel to catch the event of being created to adjust some items inside MyJPanel, like OnFormLoad in VB.  The constructor does not work since the UI elements seem to be created AFTER the constructor is created.

somePanel.validate() did not work.

The following works:


if I have the following inside MyJPanel:

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {

But it looks like a hack to me and I think there might be a standard way to do it.
Question by:rakhare
    LVL 15

    Accepted Solution

    What about overriding protected void paintComponent(Graphics g) in your JPanel class?

    Author Comment


    BINGO !!

    Author Comment


    But this has side effects.

    1. I have several MyJPanels in JScrollPane. So when I scroll, paintComponent gets called. I can bypass it with a counter/flag.

    2. There are new MyJPanels created in the non-visible portions of JScrollPane. For those, the above event is not caught. How do I catch that?

    Author Comment

    How can I add more points to this thread?
    LVL 15

    Expert Comment

    Yes, paintComponent will be called only on visible item each time the components needs to repaint itself (moved, resized,...)
    That should be similar to componentShown,no? (I don't think componentShown will send event to a non visible component).
    You can override the paintComponent (call super.paintComponent in it though) in the JScorllPange and then get all your MyJPanels from there (getComponents()).

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    wordsCount challenge 10 49
    How Complex Is This Java Course ? 9 51
    mapBully challenge 6 53
    solarwind tftp server 2 18
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now