-1

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

        }
    }
Pritpal Singh
  • 163
  • 13
  • You might like to have a look at [Model-View-Controller](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) and [How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html). In this case `static` is not going to help you – MadProgrammer Mar 18 '16 at 07:49
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Mar 18 '16 at 10:59

1 Answers1

0

I agree with @MadProgrammer, you have to study a bit the MVC and How to make dialogs and static modifier is not the solution.

A hint: you have to pass the values from MyFrame1 to MyFrame2. You can reach this for example when you are constructing a new instance of MyFrame2. You can pass the complete data in an ad-hoc bean or referencing the instance of MyFrame1.

The second option can be achived with this modifications in your code

MyFrame1.java

/**
 * Create the frame.
 */
public void action(){
    mf1=this;
    System.out.println("name-- "+mf1.tb1.getText());

    JFrame jf2 = new Myframe2(this);
    jf2.setVisible(true);
    this.dispose();
}

MyFrame2.java

private Myframe1 mf1; // non-static!
...
...
/**
 * Create the frame.
 */
public void action(){
    System.out.println("name-- "+ mf1.tb1.getText());
    tb1.setText(mf1.tb1.getText());
    tb2.setText(mf1.tb2.getText());
    tb3.setText(mf1.tb3.getText());
    tb4.setText(mf1.tb4.getText());
    tb5.setText(mf1.tb5.getText());
    mf2=this;
    btnGet.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
             if(e.getClickCount()==2){
                 dispo();
                }
        }
    });

}
...
public Myframe2(Myframe1 mf1) {
    this.mf1 = mf1;
    // Rest of your code here
}

I think that this solves your problem but you have a lot to learn about navigation between frames, the MVC, etc ...

RubioRic
  • 2,442
  • 4
  • 28
  • 35