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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:15:09+00:00 2026-05-31T13:15:09+00:00

i’m developing a JFrame which has a button to show another JFrame. On the

  • 0

i’m developing a JFrame which has a button to show another JFrame. On the second JFrame i want to override WindowsClosing event to hide this frame but not close all the application. So i do like this:

On second JFrame

addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosing(java.awt.event.WindowEvent evt) {
         formWindowClosing(evt);
    }
});

private void formWindowClosing(java.awt.event.WindowEvent evt) {
    this.dispose();
}

but application still close when i click x button on the windows. why? can you help me?

I can’t use

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

because i need to show again that JFrame with some information added in it during operations from first JFrame. So i init second JFrame with attribute visible false. if i use dispose i lose the information added in a second moment by the other JFrame. so i use

private void formWindowClosing(java.awt.event.WindowEvent evt) {
    this.setVisible(false);
}

but it still continue to terminate my entire app.

  • 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-31T13:15:10+00:00Added an answer on May 31, 2026 at 1:15 pm

    Adding a New Code with no WindowListener part as explained by @JBNizet, the very right thing. The default behaviour just hides the window, nothing is lost, you simply have to bring it back, every value inside it will remain as is, below is the sample program for further help :

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class TwoFrames
    {
        private SecondFrame secondFrame;
        private int count = 0;
    
        private void createAndDisplayGUI()
        {
            JFrame frame = new JFrame("JFRAME 1");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationByPlatform(true);
    
            secondFrame = new SecondFrame();
            secondFrame.createAndDisplayGUI();
            secondFrame.tfield.setText("I will be same everytime.");
    
            JPanel contentPane = new JPanel();  
            JButton showButton = new JButton("SHOW JFRAME 2");
            showButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    secondFrame.tfield.setText(secondFrame.tfield.getText() + count);
                    count++;
                    if (!(secondFrame.isShowing()))
                        secondFrame.setVisible(true);
                }
            });
    
            frame.add(contentPane, BorderLayout.CENTER);
            frame.add(showButton, BorderLayout.PAGE_END);
            frame.setSize(200, 200);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new TwoFrames().createAndDisplayGUI();
                }
            });
        }
    }
    
    class SecondFrame extends JFrame
    {
        private WindowAdapter windowAdapter;
        public JTextField tfield;
    
        public void createAndDisplayGUI()
        {
            setLocationByPlatform(true);
    
            JPanel contentPane = new JPanel();
    
             tfield = new JTextField(10);
    
            addWindowListener(windowAdapter);
            contentPane.add(tfield);
    
            getContentPane().add(contentPane);
            setSize(300, 300);      
        }
    }
    

    Is this what you want, try this code :

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class TwoFrames
    {
        private SecondFrame secondFrame;
    
        private void createAndDisplayGUI()
        {
            JFrame frame = new JFrame("JFRAME 1");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationByPlatform(true);
    
            secondFrame = new SecondFrame();
            secondFrame.createAndDisplayGUI();
            secondFrame.tfield.setText("I will be same everytime.");
    
            JPanel contentPane = new JPanel();  
            JButton showButton = new JButton("SHOW JFRAME 2");
            showButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    if (!(secondFrame.isShowing()))
                        secondFrame.setVisible(true);
                }
            });
    
            frame.add(contentPane, BorderLayout.CENTER);
            frame.add(showButton, BorderLayout.PAGE_END);
            frame.setSize(200, 200);
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new TwoFrames().createAndDisplayGUI();
                }
            });
        }
    }
    
    class SecondFrame extends JFrame
    {
        private WindowAdapter windowAdapter;
        public JTextField tfield;
    
        public void createAndDisplayGUI()
        {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationByPlatform(true);
    
            JPanel contentPane = new JPanel();
    
             tfield = new JTextField(10);
    
            windowAdapter = new WindowAdapter()
            {
                public void windowClosing(WindowEvent we)
                {
                    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                }
            };
    
            addWindowListener(windowAdapter);
            contentPane.add(tfield);
    
            getContentPane().add(contentPane);
            setSize(300, 300);      
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.