简体   繁体   中英

Closing a JFrame Opened in Another JFrame

I am building an application which I need to open a JFrame in another JFrame while the first JFrame is disabled.

The problem is when I want to close the second JFrame I need to enable the first JFrame.

I have tried in several ways, but it is not working properly and I was not able to reach one goal in each of them. I need to reach both goals:

  1. Disabling the first Frame when the second Frame is opened
  2. Enabling it when the second Frame is closed.

Here is part of my code:

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
    PaymentJFrame pjf = new PaymentJFrame();
    //setEnableValue(false);
    //enableFrame();
    this.setEnabled(false);
    pjf.setVisible(true);
    if (!pjf.isActive()){
        this.setEnabled(true);
    }

} 

This code dose not enable the First Frame at all. I have tried to use it in another way by adding an enable when the second Frame is closed but it is not working:

    //Class in first Frame
    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
    PaymentJFrame pjf = new PaymentJFrame();
    //setEnableValue(false);
    //enableFrame();
    this.setEnabled(false);
    pjf.setVisible(true); 
} 
    //Class in second Frame 
    private void formWindowClosed(java.awt.event.WindowEvent evt) {                                  
    // TODO add your handling code here:
    FinancialDocumentsJFrame.setEnableValue(true);
}                                 

Dose any one know how can I reach these goals?

The first Frame is the main frame and the second frame is a class that I make the frame object from it in order to show and get more information from users.

I ma using netBeans IDE designer.

First of all, a Swing application should only have one JFrame , other windows can be JDialog or somethign else. As for your question, use this code as an example. It uses a listener to detect the closing event of the second window. The following code should be in the (first) JFrame (it looks like you have a button there)

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                          

JDialog paymentDialog = new JDialog();

MyFirstFrame.this.setEnabled(false);
paymentDialog.setVisible(true);

paymentDialog.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosed(WindowEvent e) {
        MyFirstFrame.this.setEnabled(true);
    }
});
}

You can create your own dialog by extending JDialog as you did with the frames, and use the custom dialog in this code. Also instead of setting the JFrame enabled or disabled, you could consider using a modal JDialog that blocks actions to the JFrame while the dialog is active.

And further still, there is the setAlwaysOnTop(boolean) for Swing windows.

使用this.dispose()方法JFrame具有用于JFrame关闭的dispose()方法

I decided to use jDialog as it was recommended by many people on the net.

I used a jDialog and copied all the objects I had used in the second jFrame.

It worked the way I wanted it to do.

My only problem is that why java hasn't prepared a different way to answer this need.

I'm using an action listener in the first jFrame to open the second Frame which I have used jDialog for it.

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    ActivityJDialog ajdf = new ActivityJDialog(this,true);
    ajdf.setVisible(true);
} 

And I have copied everything I wanted to have into this jDialog.

public class ActivityJDialog extends java.awt.Dialog {

//Document Attributes 
int documentStatus = -1;
int documentType = -1;

/**
 * Creates new form AcrivityJDialog
 * @param parent
 * @param modal
 */
public ActivityJDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    /*if(false) // Full screen mode
    {
        // Disables decorations for this frame.
        //this.setUndecorated(true);
        // Puts the frame to full screen.
        this.setExtendedState(this.MAXIMIZED_BOTH);
    }
    else // Window mode
    {*/
        // Size of the frame.
        this.setSize(1000, 700);
        // Puts frame to center of the screen.
        this.setLocationRelativeTo(null);
        // So that frame cannot be resizable by the user.
        this.setResizable(false);
    //}
}

Thank you all for helping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM