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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:50:51+00:00 2026-05-26T07:50:51+00:00

I have a standalone Java application that gets data from a database and displays

  • 0

I have a standalone Java application that gets data from a database and displays it in a JTable. When the application starts, the user is prompted for a username/password in a JDialog. Once the correct credentials are entered, the main JFrame containing the data is displayed. On the main JFrame I have a logout button that, when clicked, should close the main JFrame and redisplay the login JDialog. Everything is mostly working except I have found that the main JFrame does not go away when the logout button is clicked. Below is a small working example of my code:

Main.java:

import javax.swing.SwingUtilities;

public class Main {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MainFrame();
            }
        });
    }
}

MainFrame.java:

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

import javax.swing.JButton;
import javax.swing.JFrame;

public class MainFrame extends JFrame implements ActionListener {
    private JButton button;
    private MyDialog dialog;
    
    public MainFrame() {
        super("this is the JFrame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        dialog = new MyDialog(this);
        button = new JButton("click me to hide this JFrame and display JDialog");
        button.addActionListener(this);
        add(button);
        pack();
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        setVisible(false); // works when changed to dispose();
        dialog.setVisible(true);
    }
}

MyDialog.java:

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

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


public class MyDialog extends JDialog implements ActionListener {
    private JFrame parentFrame;
    private JButton button;
    
    public MyDialog(JFrame parentFrame) {
        super(parentFrame, "this is the JDialog", true);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.parentFrame = parentFrame;
        button = new JButton("click me to hide JDialog and show JFrame");
        button.addActionListener(this);
        add(button);
        pack();
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        setVisible(false);
        parentFrame.setVisible(true);
    }
}

In MainFrame.java if I change the line that says setVisible(false) to dispose() then the JFrame goes away when I click the button. My question is, why does this work with dispose() and not with setVisible(false)? Is there a better way for me to organize my code? I am new to Swing so I apologize if this is a basic question. Thank you.


EDITED 2011-10-19 10:26 PDT

Thank you everyone for your help. I changed the JDialog to have a null parent and now everything works as I wanted.

  • 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-26T07:50:52+00:00Added an answer on May 26, 2026 at 7:50 am

    See the line where you initiate the JDialog:

    dialog = new MyDialog(this);
    

    You’re setting the same frame as the parent frame that the dialog sits on. You see, a dialog can’t appear on its own, it must sit on top of a parent frame.

    So in your code, when you write:

    setVisible(false); // works when changed to dispose();
    dialog.setVisible(true);
    

    In the first line you tell the frame to disappear, then you tell the dialog to appear, which really tells the dialog to appear on its parent frame. Since the parent frame is the same it looks like it stays visible to you. If you remove the second line, I’m sure the frame would disappear. But when you tell the frame to dispose, it disappears entirely because you told it not just to lose visibility, but also remove itself from memory.

    Then when you tell the dialog to appear it looks for its JFrame (which has been disposed), re-initializes it and opens up.

    The way to solve your problem is to make a separate new JFrame for the JDialog. Then don’t use dispose and just use the setVisible command.

    -Asaf

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

Sidebar

Related Questions

I have standalone Java application ,in that i have various modules Application starts from
I have a standalone Java application that uses EclipseLink 2.0.1. It is configured by
Let's say that you have a standalone application (a Java application in my case)
I have a standalone Java application below that is: Generating a random line Applied
I have written a standalone Java application that I've packaged into a jar file
I need to write a standalone Java application which will have a embedded HTTP
I have a standalone Java app that has some licensing code that I want
I have a standalone application in which I have to prompt the user with
Hai I have developed a standalone application in which when an user logs in,
Are there any standalone type conversion libraries? I have a data storage system that

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.