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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:09:43+00:00 2026-05-24T22:09:43+00:00

How can I dispose JFrame from another class? My code is listed below. public

  • 0

How can I dispose JFrame from another class? My code is listed below.

public class MainWindow

{

JFrame main_f = new JFrame("xx");
main_f.getContentPane().setLayout(new BorderLayout());
main_f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
.
.
.
}

Disposing class:

public static void DisposingJFrame (){
.
.
.
MainWindow.main_f.dispose();
}

MainWindow.main_f.dispose() won’t work because main_f isn’t a variable. Can you help me?

  • 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-24T22:09:43+00:00Added an answer on May 24, 2026 at 10:09 pm

    Suggestion:

    Make the JFrame instance a field of the MainWindow class, and provide an accessor method for it.

    public final class MainWindow{
        private final JFrame main_f;
    
        public MainWindow(){
            main_f = new JFrame("xx");
            main_f.getContentPane().setLayout(new BorderLayout());
            main_f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            .
            .
            .
        }
    
        public final JFrame getMainFrame(){
            return main_f;
        }
        .
        .
        .
    }
    

    And then in the Disposing class, you should have a MainWindow instance, where you’ll simply do the following to dispose of its JFrame instance:

    mainWindowInstance.getMainFrame().dispose();
    

    Recommendation:

    • Read the Learning the Java Language tutorial. This is basic OOP.

    Edit:

    This is to address the errors that you’re seeing:

    1. variable main_f might not have been initialized
    2. cannot find symbol “mainWindowInstance”

    With regard to the first error, this is because in the example I provided, I used the final modifier. This field must be initialized upon object creation. Therefore, you must have more than one constructor. To resolve this, either remove the final modifier, or initialize the main_f field in every constructor of MainWindow.

    With regard to the second error, mainWindowInstance is something that I left for you to create. Here’s a “for instance” –

    public class Disposing{
        private MainWindow mainWindowInstance;
    
        public Disposing(){
            mainWindowInstance = new MainWindow();
            .
            .
            .
        }
    
        public void diposeMainFrame(){
            mainWindowInstance.getMainFrame().dispose();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a member class that returned IQueryable from a data context public static
how can I dispose an <asp:ObjectDataSource> ? I mean, there is no code behind
public class MinesweeperMenu extends MinesweeperPanel{ private JPanel picture = new JPanel(); private JButton play
According this MSDN article HttpApplication .EndRequest can be used to close or dispose of
When using IDbCommand, IDataReader, or DataTable, can you depend on the destructor to dispose
How can I make sure in the following code snippet that IDataReader is disposed
I want to access JFrame(GraphicsConfiguration) using SwingBuilder.frame(), but I can't set it via the
I am registering some wrapers over un-managed objects in container. How can I dispose
I have this Java JFrame class, in which I want to use a boxlayout,
Assuming that Runtime.getRuntime().addShutdownHook(new Thread() has been set up correctly, how can the ShutdownHook thread

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.