I have an application that uses two JFrame forms - each one coming from its own class. I want to do it such that when I click on a button on the JFrame1, it hides JFrame1 (from class1) and shows JFrame2 (from class2), and vice versa. However I do not want to invoke new instances of these each time and maintain any values that have been entered into Jtext box or any variables in either forms. Currently I use Main.java that calls the Class1.
Main calls JFrameLoad
JFrameLoad calls JAdvMap AND JAdvMap calls JFrameLoad
Below is what I have:
I need help with the two lines marked with ********************
-- Main.java -----
package dl2;
/**
*
* @author Chirag
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
JFrameLoad j1 = new JFrameLoad();
j1.show();
}
}
-----JFrameLoad -----
package dl2;
import javax.swing.*;
import java.io.*;
import java.sql.*;
import java.util.StringTokenizer;
/**
*
* @author Chirag
*/
public class JFrameLoad extends javax.swing.JFrame {
public static UIManager.LookAndFeelInfo looks[];
public static Thread connectServerThread = null;
static String[] values;
public JAdvMap jm1 = new JAdvMap();
/** Creates new form JFrameLoad */
public JFrameLoad() {
initComponents();
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jtxtUsername = new javax.swing.JTextField();
jtxtPassword = new javax.swing.JPasswordField();
jbtnConnect = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jMessage = new javax.swing.JTextArea();
jTextArea1 = new javax.swing.JTextArea();
jbtnRead = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
ImageIcon icon = new ImageIcon("./dl2.gif");
setIconImage(icon.getImage()
);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent evt) {
formWindowActivated(evt);
}
});
jButton1.setLabel("Exit");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 270, -1, -1));
jLabel1.setText("Username:");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 50, -1, -1));
jLabel2.setText("Password:");
getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 80, -1, -1));
jtxtUsername.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jtxtUsernameActionPerformed(evt);
}
});
getContentPane().add(jtxtUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 50, 260, -1));
getContentPane().add(jtxtPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 80, 260, -1));
jbtnConnect.setText("Test Connection");
jbtnConnect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtnConnectActionPerformed(evt);
}
});
getContentPane().add(jbtnConnect, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 270, -1, -1));
jMessage.setLineWrap(true);
jMessage.setRows(100000000);
jMessage.setWrapStyleWord(true);
jMessage.setMinimumSize(new java.awt.Dimension(100, 17));
jMessage.setPreferredSize(new java.awt.Dimension(2, 1700000000));
jScrollPane1.setViewportView(jMessage);
getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 310, 460, 60));
getContentPane().add(jTextArea1, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 160, -1, -1));
jbtnRead.setText("Read File");
jbtnRead.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbtnReadActionPerformed(evt);
}
});
getContentPane().add(jbtnRead, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 210, -1, -1));
jButton2.setText("Click to Switch");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 210, -1, -1));
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-468)/2, (screenSize.height-409)/2, 468, 409);
}
// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// ******************** THIS IS WHAT I AM DOING TO SWITCH BETWEEN FORMS THE jm1 IS INITIALIZED AS PUBLIC IN THIS CLASS
jm1.show();
this.hide();
}
----- JAdvMap--- ----
package dl2;
/**
*
* @author chiragk
*/
public class JAdvMap extends javax.swing.JFrame {
/** Creates new form JAdvMap */
public JAdvMap() {
initComponents();
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jTest = new javax.swing.JTextField();
jBtn = new javax.swing.JButton();
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().add(jTest, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 100, 140, -1));
jBtn.setText("Click to Switch");
jBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBtnActionPerformed(evt);
}
});
getContentPane().add(jBtn, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 160, -1, -1));
pack();
}
// </editor-fold>
private void jBtnActionPerformed(java.awt.event.ActionEvent evt) {
// ********************HOW DO I MAKE SURE THAT I SHOW THE JFrameLoad THAT CALLED THIS FORM?
this.hide();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JAdvMap().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jBtn;
private javax.swing.JTextField jTest;
// End of variables declaration
}