You need to create a public static synchronized method to return the form and create it if is null.
FormA.java
/**
*
* @author joepa37
*/
public class FormA extends javax.swing.JFrame {
private static FormA f = null;
public FormA() {
initComponents();
}
public static synchronized FormA getInstance(){
try {
if (f == null) {
f = (FormA) Class.forName("FormA").newInstance();
}
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
System.out.println(e.toString());
}
return f;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
titleLbl = new javax.swing.JLabel();
goToBtn = new javax.swing.JButton();
dataTxt = new javax.swing.JTextField();
dataLbl = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
titleLbl.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
titleLbl.setText("FORM A");
goToBtn.setText("Go to FORM B");
goToBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
goToBtnActionPerformed(evt);
}
});
dataLbl.setText("Data:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(dataLbl)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(dataTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(goToBtn)
.addComponent(titleLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(94, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(titleLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(goToBtn)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(dataTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(dataLbl))
.addContainerGap(122, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void goToBtnActionPerformed(java.awt.event.ActionEvent evt) {
FormB.getInstance().setVisible(true);
this.dispose();
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
FormA.getInstance().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel dataLbl;
private javax.swing.JTextField dataTxt;
private javax.swing.JButton goToBtn;
private javax.swing.JLabel titleLbl;
// End of variables declaration
}
FormB.java
/**
*
* @author joepa37
*/
public class FormB extends javax.swing.JFrame {
private static FormB f = null;
public FormB() {
initComponents();
}
public static synchronized FormB getInstance(){
try {
if (f == null) {
f = (FormB) Class.forName("FormB").newInstance();
}
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
System.out.println(e.toString());
}
return f;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
titleLbl = new javax.swing.JLabel();
goToBtn = new javax.swing.JButton();
dataTxt = new javax.swing.JTextField();
dataLbl = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
titleLbl.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
titleLbl.setText("FORM B");
goToBtn.setText("Go to FORM A");
goToBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
goToBtnActionPerformed(evt);
}
});
dataLbl.setText("Data:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(dataLbl)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(dataTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(goToBtn)
.addComponent(titleLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(94, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(titleLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(goToBtn)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(dataTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(dataLbl))
.addContainerGap(122, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void goToBtnActionPerformed(java.awt.event.ActionEvent evt) {
FormA.getInstance().setVisible(true);
this.dispose();
}
// Variables declaration - do not modify
private javax.swing.JLabel dataLbl;
private javax.swing.JTextField dataTxt;
private javax.swing.JButton goToBtn;
private javax.swing.JLabel titleLbl;
// End of variables declaration
}
Remember to change f = (FormB) Class.forName("FormA").newInstance();
Class.forName("FormA")
and Class.forName("FormB")
Remember that the parameter string must be the path of each file.