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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:55:07+00:00 2026-05-31T10:55:07+00:00

I have a main JFrame. There is a button inside the frame. When I

  • 0

I have a main JFrame. There is a button inside the frame. When I click the button, it opens the child frame.

But I only want only 1 child frame is opend at any time, (instead whenever i click the button again , it give me the second child frame, and so on…).

So, I added actionListener to the button, to make it disable when the child frame is openning, and add windowListener to the child frame, so that When i click the close button on the top-right corner, it make the button (on the main frame) able.

Here is my code :

 import java.awt.Button;
 import java.awt.EventQueue;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 import javax.swing.JButton;

 import javax.swing.JFrame;

 public class Form1 extends JFrame implements ActionListener{
 JButton btn1=new JButton("help");

public Form1() {
    super("Form 1");
    this.add(btn1);        
    setSize(200, 200);
    btn1.addActionListener(this);
    setVisible(true);
}

public void actionPerformed(ActionEvent e) {
    if(e.getSource()==btn1){
        btn1.setEnabled(false);
        final Form2 x= new Form2();
        x.addWindowListener(new WindowAdapter(){
        @Override
        public void windowClosing(WindowEvent e){
        x.dispose();
        btn1.setEnabled(true);


    }    

    });
    }        
}

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

        @Override
        public void run() {
             new Form1();

        }
});    

}
}  

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Form2 extends JFrame {
JLabel lbl1=new JLabel("đang mở form 2 - trợ giúp");    
public Form2() {
    super();
    add(lbl1);
    setVisible(true);
    setSize(200, 200);


}



}  

So, my question is : Is there other way that I could let only one child frame opened (it means that when that child frame is opening, clicking the button in the main frame do nothing unless the that child frame is closed)?

  • 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-31T10:55:13+00:00Added an answer on May 31, 2026 at 10:55 am

    This seems like a fine way, but yes, there are other ways too. Your class could keep a reference to the child JFrame as a member variable. The button could check if that member is null or disposed, and if so, create a new one; but otherwise it could just bring the existing child to the front.

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

Sidebar

Related Questions

I have a main jFrame with the help of which i press button and
I have main categories and sub categories. I want to be able to sort
I have my main GUI thread, and a second thread running inside it's own
Example now I have a main frame contains jtable display all the customer information,
I have a basic GUI (plain Jframe with a button) and I have changed
I have a seperate JFrame where there is a text box (jTextArea) that takes
I've a maven project where there is a JFrame and I want to set
I have main table called 'Employee' and another slave table called 'EmployeeTypes' that has
i have a table called category in which i have main category ids and
I have 2 classes within same package. Both classes have main method in them.

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.