Allow only 1 (child) JFrame on screen

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

But I only want one child frame to be open at any time (instead, when I click the button again, it gives me the second child frame, etc.).

So, I added an actionListener to the button to disable it when the child frame opens, and add a windowListener to the child frame, so that when I click the close button in the top right corner, it makes the button (on the main frame).

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 any other way I could only have one child frame open (this means that when that child frame is opened, clicking the button in the main frame does nothing, unless that child frame is closed)

+3


source to share


3 answers


This seems like a great way, but yes, there are other ways. Your class can contain a reference to a child element JFrame

as a member variable. The button can check if the item is null

or is removed, and if so, create a new one; but otherwise he could have simply brought the existing child to the front.



+6


source


Use a modal dialog instead. See How to make dialogs for details .



Dialogue can be modal. When a modal dialog is displayed, it blocks user input to all other program windows . JOptionPane

creates JDialogs

that are modal. To create a modeless dialog, you must use the class directly JDialog

.

+6


source


Build Form2 ahead of time and use setVisible (true) to show it and setVisible (false) to hide it. Here's an example:

if(e.getSource()==btn1){ 
    btn1.setVisible(false); // not really needed if you disable form1 on btn1 press
    form2.setVisible(true);  // show form2
    form1.setVisible(false); // hide form1
}     

      

+2


source







All Articles