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 7820555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:20:21+00:00 2026-06-02T07:20:21+00:00

I have an application with an abstract class that extends JDialog . The class

  • 0

I have an application with an abstract class that extends JDialog. The class as an abstract void onClose(), and, in the class’s constructor, the following code is added:

addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosed(WindowEvent e) {
        onClose();
    }
}

The event is fired when expected, but then a strange thing happens. When a concrete extension of this class has code to create a new JDialog in the onClose() method, and this JDialog‘s defaultCloseOperation is JDialog.DISPOSE_ON_CLOSE, the event is fired continuously, until I force quit the operation.

I have isolated the code to the following SSCCE:

// package removed
// imports removed
public class SSCCE extends JDialog {
    public static void main(String[] args) {
        SSCCE s = new SSCCE();
        s.pack();
        s.setVisible(true);
    }
    public SSCCE() {
        setLayout(new GridLayout(1, 0, 0, 0));
        JButton btn = new JButton("click me");
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                dispose();
            }
        });
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                System.out
                        .println("SSCCE.SSCCE().new WindowAdapter() {...}.windowClosed()");
                onClose();
            }
        });
        add(btn);
    }

    public void onClose() {
        JDialog dialog = new JDialog();
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    }

}

Upon clicking the “click me” button, the blank JDialog appears and SSCCE.SSCCE().new WindowAdapter() {...}.windowClosed() appears in the console window. When I close the blank dialog, it reappears again and the text appears again.

Another really interesting thing is that when I change the initialization line from

JDialog dialog = new JDialog();

to

JDialog dialog = new JDialog() {
        @Override
        public synchronized void addWindowListener(WindowListener l) {
            super.addWindowListener(l);
            System.out
                    .println("SSCCE.onClose().new JDialog() {...}.addWindowListener()");
        }
    };

I get the following output in the console:

When clicking the “click me” button:

SSCCE.SSCCE().new WindowAdapter() {...}.windowClosed()
SSCCE.onClose().new JDialog() {...}.addWindowListener()
SSCCE.onClose().new JDialog() {...}.addWindowListener()

At the first closing of the dialog:

SSCCE.SSCCE().new WindowAdapter() {...}.windowClosed()
SSCCE.onClose().new JDialog() {...}.addWindowListener()
SSCCE.onClose().new JDialog() {...}.addWindowListener()
SSCCE.onClose().new JDialog() {...}.addWindowListener()
SSCCE.onClose().new JDialog() {...}.addWindowListener()

At the second closing of the dialog:

SSCCE.SSCCE().new WindowAdapter() {...}.windowClosed()
SSCCE.onClose().new JDialog() {...}.addWindowListener()
SSCCE.onClose().new JDialog() {...}.addWindowListener()
SSCCE.onClose().new JDialog() {...}.addWindowListener()
SSCCE.onClose().new JDialog() {...}.addWindowListener()
SSCCE.onClose().new JDialog() {...}.addWindowListener()

Each time I close the dialog, addWindowListener(WindowListener l) is called an additional time, even though it I am not intentionally calling it.

I don’t really want any WindowListeners to be registered on the dialog, but I think that simply overriding the addWindowListener(...) method and not calling super.addWindowListener(...) would be too sloppy.

I’m running Java 1.6.0_31 on Mac OS X 10.6.8, using Eclipse Indigo (with WindowBuilder, if it matters).

Does anyone have any ideas?

Thanks!

  • 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-06-02T07:20:23+00:00Added an answer on June 2, 2026 at 7:20 am

    As per the Java Dialog tutorial,

    Every dialog is dependent on a Frame component. When that Frame is destroyed, so are its dependent Dialogs.

    When you use the JDialog constructor without any arguments, it

    Creates a modeless dialog without a title and without a specified Frame owner. A shared, hidden frame will be set as the owner of the dialog.

    That shared hidden frame is SwingUtilities$SharedOwnerFrame, and on initialization it registers a WindowListener to all owned windows.

    When you close your dialog, the SharedOwnerFrame‘s windowClosed method is called, which checks for all windows it owns (at this point is the original SSCCE dialog and the new one), it finds none are visible, and so it disposes itself. This has the impact of disposing all of the dialogs it owns, which posts a window close event to each. This calls windowClosed in your listener, opening a new dialog. And round we go again :-). Regarding your last comments, you get additional log lines each time because you get one per dialog the SharedOwnerFrame owns.

    If you make the SSCCE dialog own the new dialog (by passing this into its constructor) then you don’t end up with shared ownership and it works fine:

     public void onClose() {
         JDialog dialog = new JDialog(this);
         dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
         dialog.setVisible(true);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my console application have an abstract Factory class Listener which contains code for
Problem Description I have an abstract Paper class that contains common properties of all
I have something like following: public interface A { .... } public abstract class
I have application that makes different queries with different results so the caching in
I have application that is connecting to the DB and if I enter incorrect
I have application that is up more than 3 days. I can see in
I have application 1 and application 2. App2 needs to verify that App1 is
I'm writing an application in which i have a set of code which i
I have a controller that extends Zend_Controller_Action. It contains some actions that I need
I am working on creating abstract activity classes for my application that I will

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.