• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 150
  • Last Modified:

how to select and close the window in multiple window environment

i have written a program in which i am opening multiple window for different input files, know i want to close some of the selected windows, how to select and close the window. currently i am storing the window in some array or list, latter i am comparing the title with the selected item and deleting the matching window.
 can any body help me alternate method so that i can avoid storing it in array or list.
0
Virupaksha
Asked:
Virupaksha
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Venci75Commented:
why not using Maps:
HashMap map = new HashMap();

map.put("title1", window1);
map.put("title2", window2);
map.put("title3", window3);
...
window = (Window) map.get(title2);
0
 
VirupakshaAuthor Commented:
i think if i use HashMap the memory will be same because i am using LinkedList.
   i am expecting some alternate which does not need to store or put window
0
 
TimYatesCommented:
There is know way (afaik) to enumerate child windows, like you can in Windows...

Venci75's answer is the fastest way I know of doing it
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
GurselCommented:
Well, there is a way.. Frame.getFrames() method returns
all frames created by the application...



0
 
TimYatesCommented:
oooh :-)  Cool :-)

Does it return dialogs and stuff too?

*wanders off to check the API*

>_<
0
 
VirupakshaAuthor Commented:
hello Gursel,
i got one thing to clarify, i used Frame.getFrames() but that returns static array,suppose if i call getFrames next time it shows the frames which i disposed which is a problem.
0
 
GurselCommented:
Hi Virupaksha,

There should be no reference to the frame that you dispose.. Disposing a frame only release native resources that is used by that frame.. And what is more, you have to call System.gc() after disposing.. Then Frame.getFrames() method returns all non-disposed frames.
0
 
VirupakshaAuthor Commented:
hi Gursel,
i tried with System.gc() it is still showing disposed windows.
0
 
GurselCommented:
Hi,

  Try the example . It works..



package deneme;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Frame3 extends JFrame {
    JButton btnCreate = new JButton();
    JButton btnDispose = new JButton();
    JButton btnShow = new JButton();
    JTextArea txaResult = new JTextArea();
    Frame f1;
    public Frame3() {
        try {
            jbInit();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Frame3 frame3 = new Frame3();
        frame3.setBounds(100,100,500,500);

        frame3.setTitle("Frame0");
        frame3.show();
    }
    private void jbInit() throws Exception {
        btnCreate.setBounds(new Rectangle(35, 65, 79, 27));
        btnCreate.setText("Create");
        btnCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnCreate_actionPerformed(e);
            }
        });
        this.getContentPane().setLayout(null);
        btnDispose.setBounds(new Rectangle(127, 62, 103, 27));
        btnDispose.setText("Dispose");
        btnDispose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnDispose_actionPerformed(e);
            }
        });
        btnShow.setBounds(new Rectangle(240, 62, 126, 27));
        btnShow.setToolTipText("");
        btnShow.setText("Show Frames");
        btnShow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnShow_actionPerformed(e);
            }
        });
        txaResult.setToolTipText("");
        txaResult.setBounds(new Rectangle(34, 120, 304, 118));
        this.getContentPane().add(txaResult, null);
        this.getContentPane().add(btnDispose, null);
        this.getContentPane().add(btnShow, null);
        this.getContentPane().add(btnCreate, null);
    }

    void btnCreate_actionPerformed(ActionEvent e) {
        f1 = new Frame1();
        f1.setTitle("Frame1");
        f1.show();
    }

    void btnDispose_actionPerformed(ActionEvent e) {
        if (f1!= null)
            f1.dispose();
        f1=null;
       
    }

    void btnShow_actionPerformed(ActionEvent e) {
        System.gc();
        txaResult.setText(null);
        for (int i=0;i<Frame.getFrames().length;i++) {
            txaResult.setText(txaResult.getText()+"title " +i+"th frame is "+Frame.getFrames()[i].getTitle()+"\n");

        }
    }
}




0
 
VirupakshaAuthor Commented:
hi Gursel,
it's fine,it's working.
thanks once again.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now