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);
}
});
}