0

I'm working on a program which has multiple JFrame and JDialog windows.

I have a JFrame which contains a button, when I click on this button a JDialog window opens up. In this JDialog windows there is another button, which when is clicked it opens up a second JDialog window. In the second JDialog window I have a last button.

What I want to do is to close both JDialog windows and JFrame window when this last button is clicked.

This is how the opening order is:

JFrame Frame1;
JButton Button1;

JDialog Dialog1;
JButton Button2;

JDialog Dialog2;
JButton Button3;

Button1ActionPerformed(ActionEvent e){
   new Dialog(Frame1Frame);
}

Button2ActionPerformed(ActionEvent e){
    new Dialog2(Dialog1Frame)
}

Button3ActionPerformed(ActionEvent e){
   //Here I wnat to add the code that closes JDialog2 JDialog1 and JFrame1 windows.
}

I have tried super.dispose(); but it doesn't work. Any ideas?

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Mr.E
  • 83
  • 2
  • 10

4 Answers4

2

As shown here using Action, your actionPerformed() implementation can dispatch the WINDOW_CLOSING event to the desired Window instances.

@Override
public void actionPerformed(ActionEvent e) {
    d1.dispatchEvent(new WindowEvent(d1, WindowEvent.WINDOW_CLOSING));
    d2.dispatchEvent(new WindowEvent(d2, WindowEvent.WINDOW_CLOSING));
    f1.dispatchEvent(new WindowEvent(f1, WindowEvent.WINDOW_CLOSING));
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

There may be better ways of doing this, but here is one general approach that might help.

In your code you create the windows but you do not store the reference to the windows you created into a variable. For example, you have:

JDialog Dialog1;

Then later, when you create the instance of Dialog1, you have this code:

Button1ActionPerformed(ActionEvent e){
    new Dialog(Frame1Frame);
}

This means you have created the Dialog, but you have not retained a reference to the Dialog for later manipulation by your code. If you assign this value here, you should be able to manipulate it later.

If you change your implementation to:

Button1ActionPerformed(ActionEvent e){
    Dialog1 = new Dialog(Frame1Frame);
}

Then later in your code you will have a reference to the Dialog in order to manipulate it,

Button3ActionPerformed(ActionEvent e){
   Dialog1.dispose();
   // you can manipulate the variables of the class from here and close other windows etc.
}
Mike Curry
  • 1,609
  • 1
  • 9
  • 12
1

If you have the objects reference, you can do:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;


public class Main
{
    private static JFrame frame;

    private static JButton buttonFrame;


    private static JDialog dialog1;

    private static JButton buttonDialog1;


    private static JDialog dialog2;

    private static JButton buttonDialog2;


    public static void main(String[] args) {

        /* frame */

        frame = new JFrame("Main Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);

        buttonFrame = new JButton("open dialog 1");
        buttonFrame.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                dialog1.setVisible(true);
            }
        });

        frame.add(buttonFrame);

        /* dialog 1 */

        dialog1 = new JDialog(frame, "Dialog 1");
        dialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog1.setSize(300, 300);
        dialog1.setLocationRelativeTo(null);

        buttonDialog1 = new JButton("open dialog 2");
        buttonDialog1.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                dialog2.setVisible(true);
            }
        });

        dialog1.add(buttonDialog1);

        /* dialog 2 */

        dialog2 = new JDialog(dialog1, "Dialog 2");
        dialog2.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog2.setSize(200, 200);
        dialog2.setLocationRelativeTo(null);

        buttonDialog2 = new JButton("close all");
        buttonDialog2.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                dialog2.dispose();
                dialog1.dispose();
                frame.dispose();
            }
        });

        dialog2.add(buttonDialog2);

        /* show frame */

        frame.setVisible(true);
    }
}

Otherwise you can use System.exit(0);:

buttonDialog2.addActionListener(new ActionListener() {
    @Override public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }
});
Giulio Biagini
  • 935
  • 5
  • 8
  • I tired using references but it doesn't work. Here is my code. while (resultSet.next()) { if (SessionID == resultSet.getInt(1)) { EditProfileInterface.Dialog2.dispose(); ProfileInterface.Dialog1.dispose(); DeleteAccountInterface.dispose(); new LogInInterface(); break; } Is there something wrong?/ – Mr.E Jan 18 '15 at 11:49
  • Just edited. Try the new code that shows you better how to use the `dispose()` method. It works. – Giulio Biagini Jan 18 '15 at 12:20
0

Closing multiple JFrame by another JFrame-

It is possible to close multiple windows, even non-static Java swing windows. Type below small code, step by step.

The below code is mainly for closing the First frame through the second open frame which has been opened by the same First frame.

*Make a **second class global variable** in the first-class & a **button global variable in first-class*
            
                   Class2 NiceApp1 = new Class2();
                   JToggleButton Nice=new JToggleButton("Nice One");
        
            
            // *After the Class1 above code, you will need a close method. I use a jar of NiceApplication1 with class New Close you can use the same jar or very below close method and can call the close method. You will need to type the method in Class1, just type your **close method in the method**.* 
            
                public void method (){
             
                 NewClose NiceO = new NewClose(); 
                 NiceO.Close(this);
                
                //or
                
                    Close();
               }
            
            
        *// Now make a **Button Global variable in Class2**.*
        
        javax.swing.JToggleButton Zero = new javax.swing.JToggleButton();
        
        
        *// Type in Class2 the below **method**.*   
        
        public void methodclass2(JToggleButton Nice){
          
        
         Zero = Nice;
         
        
        }
        
        
    *//Now you will need to type the below code, type the below code in the **Class1 button action** by which you will open the Class2.*  
            
        Nice.addActionListener((ActionEvent e) -> {
                
           method();
                
           });
           NiceApp1.methodclass2(Nice);
            
           NiceApp1.setVisible(true);
            
            
    *//Now just type a **single line code** in the second class, type anywhere from which you want to close Class1.*
                  
                      Zero.doClick(); 
                
                    
    *//Now the **close method** if you don't have a close method.*
                
                public void Close() {
                    WindowEvent winclosing = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
                    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winclosing);
                
                }
        

**You have done now if you did the above process.

If you only want to close which opens secondly so by the only close method change the this into your frame variables.

You can also visit NiceApplication1.BlogSpot.Com.

You can use the above code. From Class2 after the button's click, you will see your previous Class1 window will close.**

Alan
  • 9
  • 4