Question

I have two classes, and from the first jFrame1 (CotizacionGUI) I instantiate and make visible the other one (jFrame2), and I want to pass the instance of this jFrame1 (CotizacionGUI), to the other, in the constructor, to dispose it in an action triggered by the button at any moment...

public class CotizacionGUI extends javax.swing.JFrame{
    public CotizacionGUI() {
        initComponents();
    }
    private void buttonCallFrame2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        BuscarCotizacionGUI bC = new BuscarCotizacionGUI(thisjFrameinstance);
        bC.setVisible();
    }  
}

And this is the Frame2 (BuscarCotizacionGUI), here is where I want to dispose the previous jFrame, triggered by the action performed event:

public class BuscarCotizacionGUI extends javax.swing.JFrame {
    public BuscarCotizacionGUI(final JFrame otherFrame) {
        initComponents();
        this.setLocationRelativeTo(null);
        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                otherFrame.dispose();
            }
        });
    }    
}

Can you help me guys please, I don't want to do it using other class, i want to pass the reference in the jFrame1, Thanks!

Was it helpful?

Solution

The instance of first JFrame is always available to you in the same class as this

public class CotizacionGUI extends javax.swing.JFrame{
    public CotizacionGUI() {
        initComponents();
    }
    private void buttonCallFrame2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        BuscarCotizacionGUI bC = new BuscarCotizacionGUI(this);
        bC.setVisible();
    }  
}

Hope this is what you are looking for.
Good luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top