Jframe dispose, all

Hi there all,
I have a problem with my application which i developing. I couldnt find any solution. Can anybody help me?
I ll shortly explain my problem. I have a main jframe in which there are two buttons.
One button opens a new child jframe whenever i click it. And the other must close all childs jframes.But i couldnt find a way to close all opening childs jframes. One demostration i want to show you..
public class NewJFrame extends javax.swing.JFrame {
    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    new ChildFrame(jTextField1.getText());
    * @param args the command line arguments
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration
this is main andpublic class ChildFrame extends javax.swing.JFrame {
/** Creates new form ChildFrame */
public ChildFrame(String title) {
setTitle(title);
initComponents();
setVisible(true);
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
pack();
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
this is child.
i give each opening window a title,
how can i close (dispose) all opened windows with a close all buttons? Is anybody know a way to solve it?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Oh my... that didn't make much sense (at least to me).
Did you want to close all the windows and exit the virtual machine (that is, close the java.exe process) ? Normally, when all the frames close the virtual machine may exit on it's own accorde, but it's not a given. You can shutdown your aplication (which closes all the frames) with a System.exit(0) . Setting
myMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);has the same effect.
Otherwise, if you were intending to dispose all the frames, but not exit the virtual machine, then you can simply utilize the static method Frame.getFrames() . There are two similar (but not exactly the same) methods in the Window class. I think this is what was meant when you were told to read the API. You can add a window listener to the main frame. When it's closed you retreive the frames with these static methods and simply call dispose on them.

Similar Messages

  • JFrame.dispose not firing the windowsClosing event

    No this one is not quite like the question a few posts down, well not exactly anyway.
    I have a JFrame with a windowAdapter that handles my closing tasks, it works just fine currently as long as the close button on the frame is clicked. I had to add a check in the program that should close the window if its true.
    So I have code that checks a condition and if its true it shuts down all threads and trys to exit gracefully through the windowClosing event. As other posts have said I can use the jframe.dispose followed by System.exit and it works fine seeing that in the code thoughmakes me think its a kludge since dispose doesn't trigger the windowClosing event. I would like to somehow simulate clicking the close button on the frame when I need to exit because of an error but I'm not sure how to programatically do that.
    My current window close operation is set to DO_NOTHING_ON_CLOSE so that the windowClosing event can handle the closing, I tried setting the operation to EXIT_ON_CLOSE then disposing the frame when the error occured but that didn't trigger the windowsClosing event either.
    Any ideas?
    Thanks,
    Eudaemon.

    okay... So do something like this:
    private void init() { // or whatever...
       frame.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
             quit();
       quitmenuitem.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent.) {
             quit();
    public void quit() {
       // do whatever cleanup or checks or whatever...
       frame.dispose();
    }

  • JFrame close all windows and send to front

    2 JFrame questions:
    1. Is there a way to keep JFrames above all other windows? For example, if a frame was on the screen and Internet Explorer opened, the frame would stay on top of the Internet Explorer window and would still be on the screen.
    2. is there a command to close all JFrames? I have a loop that creates a new frame each time it runs. How can I say close all Boxes when the exit choice is selected?
    if (x = 1)
    JFrame frame = new JFrame ();
    Box.getContentPane ().add (new JLabel (new ImageIcon (Toolkit.getDefaultToolkit ().getImage ("Image1.gif")))); Box.setUndecorated (true);
    Box.pack (); //prepares the image
    Box.setLocation (NextPlace, 0);
    NextPlace = NextPlace + 17;
    Box.setVisible (true);
    }

    I figured it out:
    System.exit (0);
    anyway thanks for your help.
    what about the other question?
    does anyone know if there is a send to front command
    so that the JFrame is above all the other windows?System.exit(0); will close the program. But if you just want to close windows this won't work.
    Instead you can use this:
    int iterator;
         Frame[] frame = JFrame.getFrames();
              for(iterator = 0; iterator < frame.length; iterator++){
                        frame[iterator].dispose();
    This will get all windows and close them.
    Also you must add this: import java.awt.Frame;

  • Dispose all images

    Hey
    We have a very complex program that I am not allowed to show. I created a smaller project just to clear the problem. I attached the project so we can discus it. Before doing anything please read the text and follow the procedure below.
    The main.vi is the basic program. In the VA a pattern matching and golden template is used. They both use their own template located in \rastart\templates. Our program has the ability to create new templates if you press Templates button on front panel of main.vi. If you want to use the new created templates in the VA either the path needs to be changed or the program needs to be restarted (see http://forums.ni.com/t5/Machine-Vision/How-to-relo​ad-templates-in-VA/m-p/1560528#M32117). To make a restart without closing the Labview  we used this solution --> to run another VI (restart.vi) and stop the program (abort actually), the restart.vi waits for 200ms than it runs the main VI again. This works fine and the new templates are actually used in VA. No problem here. But the problem appears if we make a dispose all images just before we run the restart we. I don't know what happens, but the newly created templates are not used the second time the main.vi is running.   
    To correctly run the program I suggest the following procedure:
    - download rar file by clicking: http://dl.dropbox.com/u/2051018/rastart.rar
    - extract it to your disc
    - open the project
    - open main.vi
    - correct all the paths so the images and templates will be found (in both VAq's, inside VA - pattern matching template path and golden template path, inside Lear vision info sub vi, and other paths in main) - be careful to set all the path correctly according to where your project is located on your disc.
    - open druga folder
    - open 7.jpg with windows paint
    - draw something with black marker and save it - this is the mask that is going to be used on the templates.
    - run main.vi and observe the Image out 2 - old template is used
    - click Templates button on front panel of main.vi - new templates are created (takes some time, be patient) and restart is done
    - the new templates are used now - you can see the extra drawing on Image out 2 that you draw
    So everything was OK, the new templates were created and they were used in the VA. Now lets use Imaq dispose just before the restart.vi is started. I created a extra frame there, just put Imaq dispose inside and set the dispose all images to true. Now do the above procedure once again and you will see that the templates are not used now. I don' t know what happened. Can someone explain?!
    Cheers,
    Matej

    Hi mate47,
    I've just found this thread, still no answers after 3 months. Were you able to solve this issue?
    Best regards,
    Daniel

  • JFrame Dispose Problem

    Hello everyone,
    This is probably a really easily answerable question and I think the reason is that I have the problem is because I'm getting a stack overflow but I am not sure how to solve it.
    So, my program is a simple login and logout program
    A login JFrame creates a new Main JFrame when log in is pressed, then login is disposed.
    When a user logs out of the main frame, a new Login Frame is created and the Main Frame disposed
    The problem is that when the user logs in again, the main frame is created but the login frame remains even though dispose is called,
    why is this?
    Any help at all is greatly appreciated!
    Cheers

    why is this?You have a bug in your code.
    For more help create a [SSCCE (Short, Self Contained, Compilable and Executable, Example Program)|http://sscce.org], that demonstrates the incorrect behaviour.
    Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.

  • Question - JFrame.dispose()

    Hi all.
    Is there anybody who would explain me why the following program does not terminate? (I know I can terminate that program using System.exit(). I just want to have "some order in head".)
    public class MyFrame extends JFrame {
       public static void main(String[] args){
          new MyFrame().show();
       //this method is called after click on the button in JFrame
       public void buttonClick(){
          super.dispose();
    }many thanks
    miso

    Ohh. That a bit of a suprise. Well, then maybe this documentation from the Thread API will clear it up:
    When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:
    a) The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
    b) All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.
    I would guess the EDT is a non-daemon Thread that never finishes executing as its job is to listen for events.
    Here is a simple test program to demonstrate the above
    class ThreadDaemon
         public static void main (String args[])
              Thread thread = new Thread()
                   public void run()
                        for (int i = 0; i < 5; i++)
                             try { Thread.sleep(1000); }
                             catch(Exception e) {}
                        System.out.println("Thread Finished");
              if (args.length > 0) thread.setDaemon( true );
              System.out.println("isDaemon: " + thread.isDaemon());
              thread.start();
              System.out.println("Program Finished");
    }Run the code as follows to get two different behaviours:
    a) java ThreadDaemon d
    b) java ThreadDaemon

  • My JFrame is all white!?!?!

    I've made a JFrame and added some buttons, but for some reason it's all white? What's wrong?
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class GUI extends JFrame {
         private JButton add = new JButton("add");     
         private JButton remove = new JButton("remove");
         private JButton reset = new JButton("reset");
         private JTextArea choiceArea = new JTextArea(4,4);
         public GUI() {
              super("Generator, v1");
              Container pane = getContentPane();
              JPanel buttons = new JPanel();
              buttons.setLayout(new GridLayout(3,1));
              buttons.add(add);
              buttons.add(remove);
              buttons.add(reset);
              pane.add(buttons);
              pane.add(choiceArea);
              setSize(500,500);
              setVisible(true);          
    }

    Add the buttons to north and textarea to center. If you don't specify where to put the panels, they are by default placed in center (I think...), and because you placed the textarea there after you had placed the buttons, the buttons will be replaced by the textarea.
    this is how your code should look like:
        pane.add(buttons, BorderLayout.NORTH);
        pane.add(choiceArea, BorderLayout.CENTER);instead of
        pane.add(buttons);
        pane.add(choiceArea);

  • Cant dispose a JFrame

    hi
    i'm having a problem with jFrame disposion. i have a login JDialog, that launches JFrame. After logoff - the same JDialog appears. And when i call JFrame again - i get 2 flashing frames, that are disabling/enabling one another, and swithing from one to another. after some accurate clicks in taskbar - forms stop flicker, but i still have 2 of them.
    then you may try to logoff and login again from one of them - and guess what - i get 4 flikering frames. dammit.
    how to completely dispose Frame?
    tryed everything in forums with keyword "dispose". no luck.
    many many many thanx.

    I presume your are calling frame.dispose() from your Dialog window, and not trying to be clever by calling it in your finalizer? Additionally, you could setframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

  • JFileChooser blocks all JFrames

    I believe something like this was asked before already, but I could not find a satisfactory solution.
    Situation:
    I have X JFrames open. Each frame has a "Browse" button and a "New Window" button, and some content like an image or text. Clicking "Browse" opens a new JFileChooser, and "New Window" opens a new JFrame like all the others.
    Problem:
    Whenever I click "Browse" in one of the JFrames, I cannot interact with ANY JFrame until the JFileChooser is closed. Clicking buttons has no effect, and no ActionEvents are delivered to either parent window or other windows. Similarly, if the program cannot open the file chosen it displays an error dialog which behaves in exactly the same way, blocking all windows and not just the parent window like would be logical and acceptable.
    Failed solution:
    In the beginning of the actionPerformed method I start a new thread to actually handle the ActionEvent. I have verified that the actionPerformed method finishes before the JFileChooser is closed, so I know the opening of the filechooser is not blocking the event dispatching thread. I also ran the SwingUtilities check to see whether the JFileChooser opening was running in the event dispatching thread and the result was false.
    As far as I can see everything should be ok, but still all my JFrames remain blocked if any of them opens up a JFileChooser or a message window.
    Can anyone help?

    The dialog created by JFileChooser.showOpenDialog() is made modal
    on purpose, so that the return value can be retrieved in a single
    threaded flow without having to set listeners etc.
    You can embed a JFileChooser in your own JDialog and not make it
    modal if you want to. Add a PropertyChangeListener to catch the
    APPROVE_SELECTION and CANCEL_SELECTION events, or override
    the approveSelection() and cancelSelection() methods.
    Leif Samuelsson
    Java Swing Team
    Sun Microsystems, Inc.

  • Problem with threads in JFrame

    Hy everyone...i have a small problem when i try to insert clock in my JFrame , because all i get is an empty text field.
    What i do is that i create inner class that extends Thread and implements Runnable , but i dont know ... i saw some examples on the intrnet...but they are all for Applets...
    Does any one know how i can implement this in JFrame (JTextField in JFrame).
    Actually any material on threads in JFrame or JPanel would be great....THNX.

    For my original bad thread version, I have rewritten it mimicking javax.swing.Timer
    implementation, reducing average CPU usage to 2 - 3%.
    Will you try this:
    import javax.swing.*;
    import java.awt.*;
    import java.text.*;
    import java.util.*;
    public class SamurayClockW{
      JFrame frame;
      Container con;
      ClockTextFieldW ctf;
      public SamurayClockW(){
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        con = frame.getContentPane();
        ctf = new ClockTextFieldW();
        con.add(ctf, BorderLayout.SOUTH);
        frame.setBounds(100, 100, 300, 300);
        frame.setVisible(true);
        ctf.start();
      public static void main(String[] args){
        new SamurayClockW();
    class ClockTextFieldW extends JTextField implements Runnable{
      String clock;
      boolean running;
      public ClockTextFieldW(){
        setEditable(false);
        setHorizontalAlignment(RIGHT);
      public synchronized void start(){
        running = true;
        Thread t = new Thread(this);
        t.start();
      public synchronized void stop(){
        running = false;
      public synchronized void run(){
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        try{
          while (running){
            clock = sdf.format(new Date());
            SwingUtilities.invokeLater(new Runnable(){
              public void run(){
                setText(clock);
            try{
              wait(1000);
            catch (InterruptedException ie){
              ie.printStackTrace();
        catch (ThreadDeath td){
          running = false;
    }

  • How come the "do not dispose" option in photoshop doesn't have any effect on my stopmotion animations when I import them in After Effects?

    Hi,
    i'm trying to create an animation in photoshop. It's a sort of spiral shape that appears bit by bit.
    I'm drawing different frames, but each frame is build onto the previous frame. So i don't have to redraw to part that came before the new part:
    So first i drew all the different frames (yes they have transparent backgrounds). Then I selected all layers and went to window > animation.Here I made frames from all the layers and converted it to a frame animation. here I selected all frames and selected " do not dispose" because I want all the previous frames to stay visible when a new frame appears.
    Then  I did export >render video
    file options:
    image sequence: photoshop
    range: all frames
    render opt: straight
    frame rate: 20fps
    Now I get all the different layers as PSD files. After this I imported the files in after effects:
    file>import >multiple files 
    import as: footage, photoshop sequence, merged layers
    this gives me a single animated file with all the frames behind each other. But the "do not dispose" option has no effect here. I just shows all the different frames, but it disposes all previous frames. Instead of this as a final frame:
    I get this:
    Can someone help me with this? I'm kinda new to animation.

    An image sequence is an image sequence is an image sequence. AE does not care for specifics of PS' internal animation system and that is that. I'm also not clear why you choose to import as multiple files. A simple image sequence surely would do the trick?!
    Mylenium

  • Cant change window Icon in a JFrame

    I cannot seem to set the icon in the upper left of the window or in the task. I am using NetBeans and it created my JFrame for me using the following code to initialize:
    java.awt.EventQueue.invokeLater(new Unable() {
                public void run() {
                    new MainFrame().setVisible(true);
            });I have checked online tutorials and this seem to be a trully easy task BUT my JFrame, MainFrame, seems not to have the method I need....
    I should be able too do this:
    MainFrame.setIconImage(Image image); But it seems I cannot get that method in my JFrame at all, it just doesnt show up in NetBeans and wont allow me to compile. I have tried looking at the base JFrame class and see no setIconImage there either. Any help would be apriciated.
    KN

    Can you post the code for your MainFrame class. Use
    code tags.Ok, 1st excuse the messy code, I am teaching myself Java and the code is a bit unproffesional. Also the code is large as it is a calendar program that allows you to input text into the actual calendar day boxes, loads, saves etc. Here is the declaration of the MainFrame class and the MainFrame constructor.
    public class MainFrame extends javax.swing.JFrame {
        // Constants
        public final int DATE_HEIGHT = 17;
        public final int DAY_HEIGHT = 80;
        public final boolean DBG = true;
        public boolean Init = true;
        public int curMonth = 0;
        public int curYear = 0;
        /** Creates new form MainFrame */
        public MainFrame() {
            initComponents();
            for(int x = 0;x < 12;x = x + 2){
                jTable1.setRowHeight(x,DATE_HEIGHT);
                jTable1.setRowHeight(x+1,DAY_HEIGHT);
            jTable1.setDefaultRenderer(Object.class, new ColorCellRenderer());
            DateFormat dateFormat =
                    DateFormat.getDateInstance(DateFormat.FULL);
            // Create our Gregorian Calendar.
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(new Date());
            DaYear.setYear(cal.get(cal.YEAR));
            DaMonth.setMonth(cal.get(cal.MONTH));
            Init = false;
            LoadIt(curMonth, curYear);
        }KN

  • Positioning JButton in a JFrame

    Hello,
    I am having a problem positioning a JButton in a JFrame. All I want to do is to have a JFrame that contains a JButton at location 100,100. I dont know why I've been having trouble with this. I just want something simple, so if anyone can write a small program that extends JFrame as an example for me that would be much appreciated. Thanks for your time everyone

    For component positioning, Java use LayoutManagers which are placement algorithms.
    By default, all components have a layout manager :
    JPanels have a FlowLayout that layout the sub-components like a text document,
    The contentPane of a JFrame has a BorderLayout which divides the screen in five areas (NORTH, SOUTH,EAST, WEST and CENTER).
    If you want an absolute positioning you have to "remove" this layout manager and replace it by an "hidden" layout manager : AbsoluteLayout.
    To do this use :
    frame.getContentPane().setLayout(null);
    After that you can give absolute positions and size to your components which won't be moved or resized.
    I hope this helps,
    Denis

  • Best way to recreate all live assets in SAP and scrap current assets

    We have a situation where we have to recreate all  (15000) our assets as new assets. We will have to create new assets, provide new acquisition value and new useful life and start depreciating them from scratch. Also we will have to dispose all the current assets.
    What is the best way to accomplish this:-
    1)     I was thinking to write a BDC program for AS01 transaction to create new assets and in the text field, give a reference to old assets. 
    For Balances transfer, use transaction FB01 or F-91.
    *2)     Use BDC to map transaction AS91 and add in value there.  Although this is not transferring from legacy data but only in SAP. Will these current assets be considered as legacy data.   Is this transaction feasible to use.* 
    Also, what transaction can I use for mass asset data scrapping of current assets
    Thanks,
    Tanya
    Edited by: Tanya321 on Jul 6, 2009 7:33 PM

    Tanya,
    You can do a mass retirement of assets. Please check out this link.
    http://help.sap.com/saphelp_erp2004/helpdata/en/4f/71ece8448011d189f00000e81ddfac/content.htm
    For mass creation of assets you can use the LSMW tool.
    Regards,

  • Launching html file from JFrame

    I wish to launch an html file called "Help.html" when a user clicks on the help button on my toolbar in the GUI. I know how to open a simple program using the Runtime.exec( ) command but the problem is how would i open a specified file in a specified program...any help will be appreciated.
    PS: I do not know any C unfortunately, haven't done that yet at uni but would be glad to learn now :)

    I think you missed the question, opening a simple JFrame with a JTextArea won't have the same effect of opening a HTML file in which user can click on stuff and all that, even if I do open another JFrame imagine all the programming i have to do to get the URL's working for all the links in the help file and little search commands etc...I hope someone can tell me how to open the clients web-browser with the help.html file supplied by me!

Maybe you are looking for