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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:18:50+00:00 2026-05-30T17:18:50+00:00

I display a JDialog in a JFrame. This JDialog does nothing on dispose. I

  • 0

I display a JDialog in a JFrame. This JDialog does nothing on dispose. I would like to catch the closing event and display a Popup but nothing happens.

I cannot find the bug. Could you tell me where the problem is?

Thanks a lot!

import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;


@SuppressWarnings("serial")
public class JFrameTest extends JFrame {

    public JFrameTest() {
        setLayout(new FlowLayout());
        setSize(300, 300);
        add(new JTextArea("This is a text"));
        setDefaultCloseOperation(JFrameTest.EXIT_ON_CLOSE);
        getContentPane().setPreferredSize(getSize());
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
        JDialogTest dialog = new JDialogTest(this, Dialog.ModalityType.APPLICATION_MODAL);
        dialog.setVisible(true);
    }

    public static void main(String[] args) {
        new JFrameTest();
    }

    private class JDialogTest extends JDialog implements WindowListener {

        public JDialogTest(Window parent, ModalityType modalityType) {
            super(parent, modalityType);

            setLayout(new FlowLayout());
            add(new JLabel("This is another text"));
            setSize(200, 50);

            setDefaultCloseOperation(JDialogTest.DO_NOTHING_ON_CLOSE);
            setLocationRelativeTo(null);
            getContentPane().setPreferredSize(getSize());
            pack();
            setVisible(true);
        }

        @Override
        public void windowActivated(WindowEvent e) {}

        @Override
        public void windowClosed(WindowEvent e) {}

        @Override
        public void windowClosing(WindowEvent e) {
            JOptionPane.showMessageDialog(this, "A popup message!");
        }

        @Override
        public void windowDeactivated(WindowEvent e) {}

        @Override
        public void windowDeiconified(WindowEvent e) {}

        @Override
        public void windowIconified(WindowEvent e) {}

        @Override
        public void windowOpened(WindowEvent e) {}
    }
}
  • 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-30T17:18:51+00:00Added an answer on May 30, 2026 at 5:18 pm

    You forgot to addWindowListener to your JDialogTest class, to catch the WINDOW CLOSING event. Something like this :

    addWindowListener(this);
    

    Moreover you are calling setVisible(true) once inside your JDialogTest class and the other time while creating an object of JDialogTest class inside the JFrameTest class.

    Please never use any setXXXSize(…) method in Swing, let Layout Manager worry about that part. Moreover, it will be wise if you use setLocationByPlatform(true) instead of setLocationRelativeTo(null). A very good example as to why former should be used instead of the latter is given by @Andrew Thompson, in this Thread for How to best position Swing GUI’s

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

Sidebar

Related Questions

I would like to display details of an xml error log to a user
does Display:none or jquery Hide function speed up my website, i mean i'm developing
I'd like to display 100 floating cubes using DirectX or OpenGL . I'm looking
How would one display any add content from a dynamic aspx page? Currently I
I've a JEditorPane inside a JDialog. I'm loading a web page when this JDialog
I'm trying to display a JDialog in Windows. How do I show a JDialog
I had used JDialog box to display an Error message , JOptionPane.showMessageDialog( null, ErrorMsg,
Whenever any of my JDialogs have focus the toolbar does not display any menus
This is the regular expression I use to match phone numbers like: 00 00
Display all sub category product when click main category in magento. like Sub category

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.