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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:34:21+00:00 2026-06-04T20:34:21+00:00

I have a main JFrame window with a JButton. When I click on the

  • 0

I have a main JFrame window with a JButton. When I click on the button a new window is opened, but when I want to close the second window, both window are closed! I want the first to opened all the time. Is there a way to only close the second window? Preciate some help! Thanks!

EDIT: I add new code to show my problem. Class GUI1 has a main frame and a button to open a second frame, GUI2, that has has a button to close GUI2 frame. It’s the closing part of GUI2 I can’t solve. The code is simple and just for testing.

GUI1

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI1 extends JFrame implements ActionListener{
JButton btn1;
Container contentPane;
public GUI1()
{
    setTitle("GUI 1");
    setResizable(false);
    setSize(600,300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    btn1 = new JButton("Open GUI 2 frame");
    contentPane.add(btn1);
    btn1.setFocusable(false);
    btn1.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
    if(event.getSource() == btn1)
    {
        GUI2 frame2 = new GUI2();
        frame2.setVisible(true);
    }
}
public static void main(String[] args) {
    GUI1 frame = new GUI1();
    frame.setVisible(true);
}
}

GUI2

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI2 extends JFrame implements ActionListener {
Container contentPane;
JButton btn2;
public GUI2()
{
    setTitle("GUI 2");
    setResizable(false);
    setSize(400,200);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
    btn2 = new JButton("Close GUI 2 frame");
    contentPane.add(btn2);
    btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
    if(event.getSource() == btn2)
    {
        // Close GUI2 ??
    }
}
}
  • 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-04T20:34:23+00:00Added an answer on June 4, 2026 at 8:34 pm

    After a second read, I agree with others when they say you shouldn’t be creating and discarding JFrames (or using multiple JFrames at all). But if you really want to go that route, I’d suggest:

    • Create a single JFrame, and store a reference to it somewhere your buttons can access;
    • Show the frame when the “add” button is pressed;
    • Use JFrame.HIDE_ON_CLOSE instead of 3 (exit on close) – don’t use magic numbers;
    • Hide the frame when the other button is pressed.

    Update: the steps to apply the above suggestions to your architecture are:

    • add a field to your GUI1 class – private GUI2 frame – and only create it once (in the constructor of GUI1 for instance);
    • in your actionPerformed, only use frame.setVisible(true) – since the frame was already created;
    • I believe you could keep your DISPOSE_ON_CLOSE on GUI2, if you wanted, but HIDE_ON_CLOSE is more appropriate;
    • since it’s GUI2 – the JFrame – who is implementing the action listener, you already have a reference to it: this! Use this.setVisible(false) or simply setVisible(false).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Main app window (JFrame) with JPanel. On JPanel I have some
I have a Jframe window inside which there is a button. By clicking the
I have main categories and sub categories. I want to be able to sort
I have main.cpp (including main function) and func1.cpp, and I want to link these
I have 2 classes within same package. Both classes have main method in them.
I have a program where I have a JFrame with a JButton in it.
I have main frame (with JFrame field) asi view , then presenter (created in
I have a main controller class that shows a JFrame containing a JTable and,
I am new to java and I have two classes (class calc and button)
I'm relatively new to GUI with Java and I have a JFrame with a

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.