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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:17:21+00:00 2026-06-05T08:17:21+00:00

I have a main application frame ( MainFrame class ). On actionperformed event of

  • 0

I have a main application frame (MainFrame class). On actionperformed event of a JButton, a JPanel (MyJPanel class) is opened by placing it in JDialog. I am not extending JDialog to create MyJPanel class because I might need MyJPanel at other purposes too.

My Problem is I cannot programmatically close the MyJPanel which is displayed in JDialog. Is there anything that I missing? Could you please figure it out?

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;


    public class MainFrame extends JPanel {
        public MainFrame() {

            JButton btnOpenJdialog = new JButton("Open JDialog");
            add(btnOpenJdialog);
            btnOpenJdialog.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    JDialog jd = new JDialog();
                    MyJPanel mjp = new MyJPanel(true);//showing in JDialog
                    jd.setTitle("JDialog");
                    jd.add(mjp);
                    jd.pack();
                    jd.setVisible(true);

                }
            });
        }

        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {

                public void run() {
                    createAndShowGUI();
                }
            });
        }

        public static void createAndShowGUI() {

            JFrame frame = new JFrame("Test-JFrame");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.getContentPane().add(new MainFrame());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

    }

MyJPanel Class :

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JButton;

public class MyJPanel extends JPanel {
    private boolean isShownInJDialog = false;

    public MyJPanel() {
        JButton btnCloseMe = new JButton("Finish Action");
        add(btnCloseMe);
        btnCloseMe.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (isShownInJDialog) {
                    MyJPanel.this.setVisible(false);
                    //how to close the JDialog too.
                }
                else {
                    //just hide the content, 
                    MyJPanel.this.setVisible(false);
                }
            }
        });
    }

    public MyJPanel(boolean isShownInJDialog) {
        this();
        this.isShownInJDialog = isShownInJDialog;

    }

}

UPDATE
I was able to solve this using Howard’s answer as :

...     
if (isShownInJDialog) {
        Window w = SwingUtilities.getWindowAncestor(MyJPanel.this);
        w.setVisible(false);
}
...
  • 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-05T08:17:23+00:00Added an answer on June 5, 2026 at 8:17 am

    If I understand your question correctly, you want to close the JDialog which your MyJPanel is contained in but do not have a reference to it?

    You may either provide such a reference using the constructor of MyJPanel or change the code inside your ActionListener to

    Window w = SwingUtilities.getWindowAncestor(MyJPanel.this);
    w.setVisible(false);
    

    which looks up the parent window of your panel without direct reference.

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

Sidebar

Related Questions

In my main application I have a result event and an XMLList , which
I have a JDialog as the main window in my application (originally it was
I have my main application delegate which contains a method that returns an object.
I have my main application ,from my main application I will be calling another
I have a main application, and a bunch of sub-applications (they are separate apps,
I have a java main application, and I want to use log4j. I don't
So I have a UIViewController (main application controller is a TabBarController). On this there
I have a List in the main application and I am trying to access
I have an update program that is completely independent of my main application. I
I have WPF Application where I have One main form and other user controls

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.