0

Hi i was wondering how to Dispose a jFrame from another one, cause i want to create a new instance of that class with new values in its textfields, so the First jFrame is this:

public class Frame1 extends javax.swing.JFrame implements ActionListener {
    Frame2 f;
    public Frame1() {
        initComponents();
        this.setLocationRelativeTo(null);
    }
    private void rbtnShowFrame2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        f = new Frame2();
        f.setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
        this.dispose(); //I TRIED TO DISPOSING IT HERE BUT DOESN'T WORK
    }
}

So i want in the other jFrame Dispose the jFrame1 only if i trigger the event action performed of a botton, if this doesn't happen i do't want to dispose it, i don't know if i can do it with ActionListener, this is the Second jFrame:

public class Frame2 extends javax.swing.JFrame {
    public Frame2() {
        initComponents();
        this.setLocationRelativeTo(null);
        Frame1 f1 = new Frame1();
        this.cmdOk.addActionListener(cGUI);
    }
    private void cmdOkActionPerformed(java.awt.event.ActionEvent evt) {                                         
        //Here is where i want to dispose() the other jFrame1
        //to create a new instance and pass the value using public static jTextFields
        f1.labelNumeroCotizacion.setText(this.labelNumCotizacionEnviar.getText());
        f1.setVisible(true);
    }
} 

Sorry for my Code, i am newbie using OOP! thanks for all guys....

Mario
  • 51
  • 8
  • Sidenote: Do you really want to replace one frame with another? How about just replacing the panel inside? Or if it's just about text field values, how about calling `set` methods on the fields in your frame. – Stroboskop Feb 28 '14 at 14:42
  • i just really want to change the values in the jTextFields, but i dont know if i can do this only using setters and getters, cause the only way i have discovered is using a new instance, but i don't know if you can help me bring me another idea, thanks for your answer! – Mario Feb 28 '14 at 14:47
  • Make member variables of your text fields in Frame1. Then give Frame1 either a set method for each of the fields that will call `field.setText(text)` or give the frame one big `setData` method with a composite object containing all the values as members. – Stroboskop Feb 28 '14 at 16:21
  • Ok i understand, the problem that i have is that i have to create an instance from the other class, to do the getters and setters of the jTextFields, so i have to create a new object, and i cant pass the values to the earlier jFrame... – Mario Feb 28 '14 at 16:30
  • You would need to keep a reference to your instance of Frame1 in Frame2 either way. It's the same for wanting to dispose it or for using the setters. Look at Awefully Awesome's code. He's passing the instance of FrameA to the constructor of FrameB. – Stroboskop Feb 28 '14 at 16:43
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 2) For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Mar 01 '14 at 01:20

1 Answers1

2

Here is an example of how to dispose a JFrame from another JFrame:

import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Demo
{
  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable()
    {
      @Override
      public void run()
      {
        FrameA one = new FrameA();
        FrameB two = new FrameB(one);

        one.setVisible(true);
        two.setVisible(true);
      }
    });
  }
}

class FrameA extends JFrame
{
  private static final long serialVersionUID = 1812279930292019387L;

  public FrameA()
  {
    super("Frame A");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 400);
    setLocationRelativeTo(null);

    setResizable(false);
  }
}

class FrameB extends JFrame
{
  private static final long serialVersionUID = 5126089271972476434L;

  public FrameB(final JFrame otherFrame)
  {
    super("Frame B");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 400);
    setLayout(new GridBagLayout());
    setLocationRelativeTo(otherFrame);

    JButton button = new JButton("Dispose Other");
    button.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        otherFrame.dispose();
      }
    });

    add(button);

    setResizable(false);
  }
}
Aman Agnihotri
  • 2,973
  • 1
  • 18
  • 22
  • Cool, and if your question is answered by this question, then accept it to close the question. Or ask any further doubts in the comments section if you have any. – Aman Agnihotri Feb 28 '14 at 14:49
  • Look, i don't know where to put that code, in my case i want to close jFrame1 in the button of jFrame2, so can you give me a clue how to put that code into mine? cause i already tried but i cant doit! – Mario Feb 28 '14 at 15:02
  • 1
    If you see my code again, then `FrameA` is being closed on click of a button which is present in `FrameB`. You add an `actionListener` to your `button`, and then call the `dispose` method on the `jFrame1`. You need to pass a reference of `jFrame1` to your `jFrame2` class, so that it can use it in the `button`'s `actionPerformed` method. – Aman Agnihotri Feb 28 '14 at 15:09
  • Copy paste my code in your IDE and compile and run it to get the idea. – Aman Agnihotri Feb 28 '14 at 15:11