[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Closing Frames

Posted on 2009-04-20
4
Medium Priority
?
162 Views
Last Modified: 2013-11-23
How to close more than one frame at a time.Please give code
0
Comment
Question by:narengoogol
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 24182647
there is no method to do that. you need to simple close them one at a time

frame1.setVisible(false);
frame2.setVisible(false);
frame3.setVisible(false);

0
 
LVL 1

Assisted Solution

by:kingkaun
kingkaun earned 1000 total points
ID: 24671135
I guess your application uses more than one frame and you want to close all the frames when a user clicks on the close icon on one of them. The easiest way to do that is to write a custom window listener or adapter and add them to all the frames.  In your custom window adapter you can hold the reference to all the frames and close all of them when one is closed.

//Pseudo Code
MyWindoAdapter extends WindowAdapter{
List allFrames = new ArrayList();
public void addFrame(JFrame frame){
  allFrames.add(frame);
}
 
public void removeFrame(JFrame frame){
   allFrames.remove(frame);
}
//@override
public void windowClosing(WindowEvent e){
  Iterator iter = allFrames.iterator();
  while(iter.hasNext()){
   JFrame f = (JFrame)iter.next();
    f.setVisible(false);
 }
}
 
}

Open in new window

0
 
LVL 10

Expert Comment

by:Pramod Kumar
ID: 25344068
Try this:

import javax.swing.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
 
/**
 * Created by IntelliJ IDEA.
 * User: kpramod
 * Date: Sep 16, 2009
 * Time: 4:12:54 PM
 */
public class MultiFrame implements ActionListener {
    ArrayList <JFrame>frames = new ArrayList<JFrame>();
 
    JFrame f;
    JPanel p;
    JButton add,closeAll;
 
    public MultiFrame(){
        f = new JFrame("Multiple Frames");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        p = new JPanel();
        add = new JButton("Add New Frame");
        closeAll = new JButton("Close All");
 
        add.addActionListener(this);
        closeAll.addActionListener(this);
 
        p.add(add);
        p.add(closeAll);
 
        f.getContentPane().add(p, BorderLayout.SOUTH);
        f.setSize(300,200);
        f.setVisible(true);
    }
    
    public static void main(String[] args) {
        new MultiFrame();
    }
 
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==add){
            JFrame ff = new JFrame("New Frame "+(frames.size()+1));
            ff.setSize(200,200);
            ff.setVisible(true);
            frames.add(ff);
        }
        if(e.getSource()==closeAll){
            Iterator it = frames.iterator();
            while(it.hasNext()){
                JFrame currFrame = (JFrame)it.next();
                currFrame.dispose();
            }
        }
    }
}

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question