0

I have two JFrames (frameA and FrameB). frameB can only be opened from frameA and when I open frameB, frameA must be left open. frameB has got a button (Close_frameA). I would like when the button is clicked to close frameA.

How can i do it?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366

2 Answers2

1

First of all, is each JFrame made by a different class( I would assume so because I don't know any other way to make two frames).

Possible solution to try: in frame A, create a "static variable":

//lets call the class that create frameA ClassA
public class ClassA extends JFrame {
    static JFrame frameA;

instead of doing

JFrame frameA=new JFrame("Name of the frame");

in the public static void main(String[] args).Then, in the public static void main(String[] args) program, do

//the static JFrame assigned before
frameA= new JFrmae("Nameof the frame");

this lets the program in frameB to read "frameA" with the following code in ClassB(lets call the class that make frameB ClassB):

JFrame frameA= ClassA.frameA;

then, still in ClassB, we can do

frameA.dispose();

I hope you understand(please comment for what you don't understand if you don't), and i hope it works.

Code:

import javax.swing.JFrame;

public class ClassA {
    static JFrame frameA;
    public ClassA(){
        //a useless constructor because I am not adding any Listeners(don't worry about it)
    }
    public static void main(String[] args){
        frameA=new JFrame("Name");

        //your ordinary things(some peiople put these in the constructor)
        frameA.setSize(300,300);
        frameA.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frameA.setVisible(true);
        //runs ClassB
        new ClassB();
    }
}

and

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

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


public class ClassB extends JFrame implements ActionListener{
    static JButton close=new JButton("close");
    public ClassB(){

        //your ordinary thigns
        add(close);
        setSize(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        close.addActionListener(this);
        System.out.println("what?");
    }

    public static void main(String[] args){
        JFrame frameB=new JFrame("Clae Frame A");
    }
    @Override
    public void actionPerformed(ActionEvent arg0) {
        if(arg0.equals("close")){
            JFrame frameA=ClassA.frameA;
            frameA.dispose();
        }

    }

}
Normen Yu
  • 79
  • 11
0

You can use below two class: TJFrame and OpenFrame to close a JFrame class with a button in another JFrame

 public class TJFrame {
public static OpenFrame openWindow;

public static void main(String[] args) {
    JFrame frame = new JFrame("Swing Frame");
    JButton button = new JButton("Open");
    frame.add(button);
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            openWindow = new OpenFrame();
            openWindow.setVisible(true);
        }
    });
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    frame.setSize(350, 200);
    frame.setVisible(true);
    }}
  public class OpenFrame extends JFrame{

JPanel back_panel;
public JButton button = new JButton("Cross");

public OpenFrame() {
    back_panel = new JPanel();
    setContentPane(back_panel);
    this.setSize(350, 200);

    button.setBounds(380, 10, 20, 20);
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            dispose();

        }
    });

    back_panel.add(button);
}}
Rafiq
  • 740
  • 1
  • 5
  • 17