I want To Pass the copy of whole object to a new Jframe and when i return back from that new Jframe i have my previous session values as it is in my text boxes previously. I mean my data entered will not be lost in any case if someone accidentally clicked Next button.
Here Is my Frame1 Code
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Myframe1 extends JFrame {
private JPanel contentPane;
public JTextField tb1;
public JTextField tb2;
public JTextField tb3;
public JTextField tb4;
public JTextField tb5;
JLabel lblName = new JLabel("name");
JLabel lblRoll = new JLabel("Roll");
JLabel lblClass = new JLabel("Class");
JLabel lblSection = new JLabel("Section");
JLabel lblMarks = new JLabel("Marks");
JButton btnNext = new JButton("Next");
static Myframe1 mf1=null;
static Myframe2 mf2=null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Myframe1 frame = new Myframe1();
frame.setTitle("Frame 1");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public void action(){
mf1=this;
System.out.println("name-- "+mf1.tb1.getText());
new Myframe2().show();
this.dispose();
}
public void hodData(){
// if(mf1==null){
// mf1=Myframe2.holdData;
// }
}
public Myframe1() {
hodData();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 275, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
tb1 = new JTextField("Name");
tb1.setBounds(119, 22, 114, 19);
contentPane.add(tb1);
tb1.setColumns(10);
tb2 = new JTextField("Roll");
tb2.setColumns(10);
tb2.setBounds(119, 53, 114, 19);
contentPane.add(tb2);
tb3 = new JTextField("Class 43");
tb3.setColumns(10);
tb3.setBounds(119, 84, 114, 19);
contentPane.add(tb3);
tb4 = new JTextField("Section");
tb4.setColumns(10);
tb4.setBounds(119, 115, 114, 19);
contentPane.add(tb4);
tb5 = new JTextField("Marks");
tb5.setColumns(10);
tb5.setBounds(119, 146, 114, 19);
contentPane.add(tb5);
lblName.setBounds(31, 24, 70, 15);
contentPane.add(lblName);
lblRoll.setBounds(31, 55, 70, 15);
contentPane.add(lblRoll);
lblClass.setBounds(31, 86, 70, 15);
contentPane.add(lblClass);
lblSection.setBounds(31, 117, 70, 15);
contentPane.add(lblSection);
lblMarks.setBounds(31, 148, 70, 15);
contentPane.add(lblMarks);
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action();
}
});
btnNext.setBounds(31, 177, 202, 25);
contentPane.add(btnNext);
}
}
Here Is my Frame2 Code
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Myframe2 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
public JTextField tb1;
public JTextField tb2;
public JTextField tb3;
public JTextField tb4;
public JTextField tb5;
JLabel lblName = new JLabel("name --");
JLabel lblRoll = new JLabel("Roll --");
JLabel lblClass = new JLabel("Class --");
JLabel lblSection = new JLabel("Section --");
JLabel lblMarks = new JLabel("Marks --");
JButton btnGet = new JButton("Get Data ");
static Myframe2 mf2=null;
Myframe1 mf1=new Myframe1();
static Myframe1 holdData=Myframe1.mf1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Myframe2 frame = new Myframe2();
frame.setTitle("Frame 2");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public void action(){
System.out.println("name-- "+Myframe1.mf1.tb1.getText());
tb1.setText(Myframe1.mf1.tb1.getText());
tb2.setText(Myframe1.mf1.tb2.getText());
tb3.setText(Myframe1.mf1.tb3.getText());
tb4.setText(Myframe1.mf1.tb4.getText());
tb5.setText(Myframe1.mf1.tb5.getText());
mf2=this;
btnGet.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2){
dispo();
}
}
});
}
public void dispo(){
this.hide();
mf1.show();
}
public Myframe2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 275, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
tb1 = new JTextField();
tb1.setBounds(119, 22, 114, 19);
contentPane.add(tb1);
tb1.setColumns(10);
tb2 = new JTextField();
tb2.setColumns(10);
tb2.setBounds(119, 53, 114, 19);
contentPane.add(tb2);
tb3 = new JTextField();
tb3.setColumns(10);
tb3.setBounds(119, 84, 114, 19);
contentPane.add(tb3);
tb4 = new JTextField();
tb4.setColumns(10);
tb4.setBounds(119, 115, 114, 19);
contentPane.add(tb4);
tb5 = new JTextField();
tb5.setColumns(10);
tb5.setBounds(119, 146, 114, 19);
contentPane.add(tb5);
lblName.setBounds(31, 24, 70, 15);
contentPane.add(lblName);
lblRoll.setBounds(31, 55, 70, 15);
contentPane.add(lblRoll);
lblClass.setBounds(31, 86, 70, 15);
contentPane.add(lblClass);
lblSection.setBounds(31, 117, 70, 15);
contentPane.add(lblSection);
lblMarks.setBounds(31, 148, 70, 15);
contentPane.add(lblMarks);
btnGet.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action();
}
});
btnGet.setBounds(31, 177, 202, 25);
contentPane.add(btnGet);
}
}