Note that there are some explanatory texts on larger screens.

plurals
  1. POAllow only 1 (child) JFrame on the screen
    primarykey
    data
    text
    <p>I have a main JFrame. There is a button inside the frame. When I click the button, it opens the child frame.</p> <p>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...). </p> <p>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.</p> <p>Here is my code :</p> <pre><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); } } </code></pre> <p>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)?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload