0

i created one frame in that having one ok Button. if click on this button, the frame window should close and destroy the memory of the frame window, after that it will open another frame, while clicking that ok button.

Is the code below correct?

public class GlobalVariableClass1 extends javax.swing.JFrame {
        public static String GVar;

        public GlobalVariableClass1() {
            initComponents();
         }

        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {

            jPanel2 = new javax.swing.JPanel();
            Cmd01 = new javax.swing.JButton();
            jPanel1 = new javax.swing.JPanel();
            Txt01 = new javax.swing.JTextField();
            Lbl02 = new javax.swing.JLabel();
            Lbl01 = new javax.swing.JLabel();
            Lbl03 = new javax.swing.JLabel();

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {
                    formWindowClosing(evt);
                }
            });
            getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

            jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, java.awt.Color.darkGray), "Commands", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 12))); // NOI18N
            jPanel2.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

            Cmd01.setText("Class B");
            Cmd01.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    Cmd01ActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(20, 20, 20)
                    .addComponent(Cmd01)
                    .addContainerGap(33, Short.MAX_VALUE))
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(Cmd01, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
                    .addContainerGap())
            );

            getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 10, 130, 80));

            jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, java.awt.Color.darkGray), "GlobalValue", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 12))); // NOI18N

            Txt01.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

            Lbl01.setText("Value Entered in C");

            Lbl03.setText("Enter Value");

            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(Lbl03, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
                            .addGap(43, 43, 43)
                            .addComponent(Txt01, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(29, 29, 29))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(Lbl01, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                            .addGap(18, 18, 18)
                            .addComponent(Lbl02, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(Lbl02, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE))
                        .addComponent(Lbl01, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE))
                    .addGap(34, 34, 34)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(Lbl03, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Txt01, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(36, 36, 36))
            );

            getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 120, 260, 160));

            pack();
        }// </editor-fold>                        
     // ** this is the way i used, is it correct:**
        private void Cmd01ActionPerformed(java.awt.event.ActionEvent evt) {                                      
                GVar=Txt01.getText();
                dispose();
                new GlobalVariableClass2().setVisible(true);
        }                                     

        private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   


        }                                  
           public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new GlobalVariableClass1().setVisible(true);
                }
            });
        }
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
javasi
  • 25
  • 8
  • Does it do what you want? If so, what makes you think it could not be correct? – JB Nizet Jan 21 '12 at 11:08
  • 3
    You should not be creating multiple frames. That is a path to madness. Look to the advice of mKorbel on your [previous thread](http://stackoverflow.com/questions/8952038/how-to-call-formwindowclosing-event-in-buttonactionperformevent) re `CardLayout`. Also look into `JDesktopPane`/`JInternalFrame`, `JTabbedPane`, `JSplitPane`, a combination of one `JFrame` and `JDialog` instances.. – Andrew Thompson Jan 21 '12 at 11:09

1 Answers1

1

If you never assign the instance of GlobalVariableClass1 to any variable or field in another class, dispose() will do what you want. For example:

new GlobalVariableClass1().setVisible(true);

will create a temporary variable to save the result of new and then use the temporary to call setVisible(). Since the variable is only temporary, Java will forget about it as soon as it sees the ; (so to speak).

This will cut all ties to the instance created with new and the GC will be able to collect it (= free the memory).

If you're still confused, GC works like this:

  • It iterates over all objects in memory and assigns each a counter with the value 0.
  • It then looks for all references anywhere (= fields and local variables that don't have a primitive type).
  • It will follow each reference and increment the counter for the respective instance.
  • For all instances with a counter == 0, the instance will be "collected" since there is no way for anyone to reach this instance anymore (no reference anywhere in the VM points to it).

Of course, life of a GC in 2012 is much more complex but this is, in very simple terms, how GC always works.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • please read http://stackoverflow.com/questions/6309407/remove-top-level-container-on-runtime +1 for rest – mKorbel Jan 21 '12 at 11:22
  • Well, of course, under the hood Java AWT will be set up for the first frame and a lot of that will stick around, so some parts of the frame will stick around until the VM is terminated, too. But I felt that would be too much detail :-) – Aaron Digulla Jan 21 '12 at 11:36