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

  • SEARCH
  • Home
  • 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 7014343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:30:33+00:00 2026-05-27T22:30:33+00:00

I create a Popup using the PopupFactory.getPopup method. According to the documentation, I am

  • 0

I create a Popup using the PopupFactory.getPopup method. According to the documentation, I am required to call the hide() method on the popup when it is no longer needed.

In my application, the popup is the child of a JLabel which may be removed from the current frame in a number of different situations. (Either the JLabel itself or one of its parent containers is removed.) Rather that calling hide() in every single place (and making the Popup object available in all these places) I would prefer to be able to detect the removal of the JLabel or one of its parent containers.

How can I detect the removal? I naively assumed that the removal of a component meant the removal/hiding of its children, but as the code below shows, the popup survives the removal of the JLabel.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Xyzzy extends JFrame {
    static Xyzzy frame;
    static JPanel panel;
    static JLabel text1;
    static JLabel text2;

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    frame = new Xyzzy();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));

                    panel = new JPanel();
                    panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
                    frame.add(panel);


                    text1 = new JLabel("text1");
                    text2 = new JLabel("text2");

                    panel.add(text1);

                    frame.add(new JButton(new AbstractAction("Add popup") {
                            public void actionPerformed(ActionEvent e) {
                                PopupFactory factory = PopupFactory.getSharedInstance();
                                Popup popup = factory.getPopup(text1, new JLabel("POPUP"),frame.getX()+300,frame.getY()+300);
                                popup.show();
                            }
                        }));

                    frame.add(new JButton(new AbstractAction("New label") {
                            public void actionPerformed(ActionEvent e) {
                                panel.remove(text1);
                                panel.add(text2);
                                panel.revalidate();
                            }
                        }));

                    frame.setSize(600, 600);
                    frame.setVisible(true);
                }
            });
    }
}

This code creates a JFrame displaying the text “text1” and two buttons. If you press the button labeled “Add popup”, a Popup with the text “POPUP” appears in the window. This Popup is a child of text1. Press the “New label” button and “text1” is removed from the display, but the Popup survives.

I need to be able to detect when text1 or the containing panel is removed so that I can hide the popup. I want to avoid adding code where the actual remove() method is called.

  • 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-27T22:30:33+00:00Added an answer on May 27, 2026 at 10:30 pm

    You can use HierarchyListener:

    public void actionPerformed(ActionEvent e) {
      PopupFactory factory = PopupFactory.getSharedInstance();
      final Popup popup = factory.getPopup(text1, new JLabel("POPUP"),frame.getX()+300,frame.getY()+300);
      text1.addHierarchyListener(new HierarchyListener() {
    
        public void hierarchyChanged(HierarchyEvent e) {
          if (e.getID() == HierarchyEvent.HIERARCHY_CHANGED
              && (e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) {
            popup.hide();
          }
        }
      });
      popup.show();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I create a simple popup menu like Facebook's hide story popup using
I am trying to create popup using the jqueryui dialog. I am trying to
Using WPF .NET 4.0 in VS2010 RTM: I can't create a fullscreen WPF popup.
I am newly using GWT. I am using a simple code to create popup
I'm using Netbeans to develop a Java application and I want to create a
I am using the code from How to create a stunning and smooth popup
Im trying to create a browser popup window with some text, rather than using
I want to create modal popup box using jquery depending on the clicked tr
I want to create a popup window using wxPython that acts like a bash
i am using jquery.simplemodal-1.1.1 and jquery-1.2.6.min to create a modal popup. this is my

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.