Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4536926
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:36:09+00:00 2026-05-21T14:36:09+00:00

I am trying to capture the very first moment when a component is shown

  • 0

I am trying to capture the very first moment when a component is shown on the screen without using ‘dirty’ solutions as with use of a timer.
Basically, I want to know the moment when I can safely start using getLocationOnScreen() method on the component.

I thought that component listener could help but no luck here. I am stuck for now and do not know which listener to use for this. Any suggestions?

Here is some sample code which shows that a component listener fails.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CompListenerTest
{
    static ComponentListener cL = new ComponentAdapter()
    {
        @Override
        public void componentShown(ComponentEvent e)
        {
            super.componentShown(e);
            System.out.println("componentShown");
        }
    };

    static MouseListener mL = new MouseAdapter() 
    {

        @Override
        public void mousePressed(MouseEvent e)
        {
            super.mousePressed(e);
            JComponent c = (JComponent) e.getSource();
            System.out.println("mousePressed c="+c.isShowing());
        }

    };

    public static void main(String[] args)
    {
        JPanel p = new JPanel();
        p.setPreferredSize(new Dimension(300, 400));
        p.setBackground(Color.GREEN);
        p.addComponentListener(cL);
        p.addMouseListener(mL);

        System.out.println("initial test p="+p.isShowing());
        JPanel contentPane = new JPanel();
        contentPane.setBackground(Color.RED);
        contentPane.add(p);
        JFrame f = new JFrame();
        f.setContentPane(contentPane);
        f.setSize(800, 600);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T14:36:09+00:00Added an answer on May 21, 2026 at 2:36 pm

    The reason a ComponentListener doesn’t work is that it reports changes to the visible property – and that is true by default, even without being part of the component hierarchy.

    To be reliably notified, use a HierarchyListener


    Edit (musings about my knowledge evolution in regard to this question/answers, not sure what the netiquette has to say about doing it … simply guide me if that’s the wrong way to go 🙂

    First: the question as asked in the subject is not necessarily related to the actual problem (as commented by Boro below – any way to link to a comment?): there’s no need to keep some kind of local flag to decide whether or not it is safe to send a getLocationOnScreen to a component, simply ask the component itself. Learn-item 1 for myself 🙂

    Second: The question as asked is quite interesting. Five experts (including myself, self-proclaimed), five different answers. Which triggered a bit of digging on my part.

    My hypothesis: ComponentEvents are not useful for notification of (first-)showing. I knew that componentShown is useless because it’s a kind-of propertyChange notification of the visible property of a component (which rarely changes). Puzzled about the suggested usefulness of moved/resized, though.

    Constructing a use-case: fully prepare the frame in the example and keep it ready for later showing, a typical approach to improve perceived performance. My prediction – based on my hypothesis: resized/moved fired at prepare-time, nothing at show-time (note: the isShowing is what we are after, that is the latter). A snippet to add in the OP’s example:

        final JFrame f = new JFrame();
        f.setContentPane(contentPane);
        f.setSize(800, 600);
        //        f.pack(); 
    
        JFrame controller = new JFrame("opener");
        controller.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Action open = new AbstractAction("open/hide second") {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                f.setVisible(!f.isVisible());
            }
    
        };
        controller.add(new JButton(open));
        controller.pack();
        controller.setVisible(true);
    

    Disappointment: no notification at prepare-time, notification at show-time, just as needed, my hypothesis seemed wrong 😉 Last chance: swap the setSize for a pack … and voila, notification at prepare-time, no notification at show-time, happy me again. Playing a bit more: looks like ComponentEvents are fired if the a component is displayable, which may or may not be useful in some contexts but not if showing is the state we are after. The

    New imperial rules (draft):
    Do not use ComponentListener for notification of “showing”. That’s left-over from AWT-age.
    Do use AncestorListener. That seems to be the Swing replacement, slightly misnomed notification of “added” which actually means “showing”
    Do use HierarchyListener only if really interested in fine-grained state changes

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make an application in which one component captures the screen
I'm trying to capture certain parts of HTML using regular expressions, and I've come
I am trying to capture output from an install script (that uses scp) and
I have an application in which I'm trying to capture the shift key modifier
I'm trying to do an image capture on a high end Nokia phone (N95).
I'm trying to write a regex to parse a (seemingly very simple) piece of
I am trying to use databinding to bind data to a Silverlight toolkit chart.
Hey all, I am trying to accomplish something very simple yet getting an error
I'm trying to capture and handle the return of a NullPointerException in my DWR
I am trying to capture / extract numeric values from some strings. Here is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.