-1

I have a Java application that should display Gui1 then go to Gui 2 which I successful did , Then from Gui2 go back to Gui 1 which I have problems in doing. So how can I go back to Gui 1 when I press a button in Gui2

Gui1 code

     import java.awt.event.ActionEvent;
     import java.awt.event.ActionListener;
     import javax.swing.JButton;
     import javax.swing.JFrame;
     import javax.swing.SwingUtilities;
     public class Gui1 extends JFrame {   
     private JButton btn=new JButton("Open Gui2");

    public Gui1()
    {
        super(" GUI1");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        btn.addActionListener(new ButtonListener());
        btn.setActionCommand("Open");
        add(btn);
         }
 class ButtonListener implements ActionListener {
         public void actionPerformed(ActionEvent e)
            {
                String cmd = e.getActionCommand();

                if(cmd.equals("Open"))
                {
                    dispose();

                    new Gui2();
                }
            } 
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable(){


            public void run()
            {
                new Gui1().setVisible(true);
            }

        });
    }
}

Gui2 code

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Gui2 extends JFrame
{
    private JButton btn1= new JButton("Go Back to Gui 1");
    public Gui2()
    {
        super("Another GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        add(btn1);

        setVisible(true);
    }
}
sara
  • 534
  • 1
  • 9
  • 22
  • the `Gui1` state must be saved? – Jordi Castilla Jun 16 '15 at 07:36
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Jun 16 '15 at 07:46
  • As an aside: Using `setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);` in both frames will cause an unwelcome surprise when you do manage to close one. ;) Also.. Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Jun 16 '15 at 07:48

4 Answers4

2

To go back use the code below:

  public class Gui2 extends JFrame {
 private JButton btn1 = new JButton("Go Back to Gui 1");

public Gui2() {
    super("Another GUI");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    add(btn1);
    btn1.addActionListener(new Gui2.ButtonListener());
    btn1.setActionCommand("back");
    setVisible(true);
}

private class ButtonListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();

        if (cmd.equals("back")) {
            dispose();

            new Gui1().setVisible(true);
        }
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Rafiq
  • 740
  • 1
  • 5
  • 17
0

To achieve this, you could try to pass the reference of your Gui1 class into the constructor of the Gui2 class and create a private attribute which store your first window. Then just create a button that implements ActionListener and hide/show the desired window.

Sw4Tish
  • 202
  • 4
  • 12
0

Use the ButtonListener for both classes. There ist still room for improvements, but this is a solution based on your code, that works. The ButtonListener now takes the calling JFrame as argument to close it when needed.

I also changed the location of setVisible(true); from the main() to both constructors.

Gui1

public class Gui1 extends JFrame {
    private JButton btn = new JButton("Open Gui2");

    public Gui1() {
        super(" GUI1");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btn.addActionListener(new ButtonListener(this));
        btn.setActionCommand("Open");
        add(btn);

        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new Gui1();
            }

        });
    }
}

Gui2

public class Gui2 extends JFrame {
    private JButton btn1 = new JButton("Go Back to Gui 1");

    public Gui2() {
        super("Another GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btn1.addActionListener(new ButtonListener(this));
        btn1.setActionCommand("Open");
        add(btn1);

        setVisible(true);
    }
}

ButtonListener

public class ButtonListener implements ActionListener {
    JFrame caller = null;

    public ButtonListener(JFrame caller) {
        this.caller = caller;
    }

    public ButtonListener() {}

    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();

        if (cmd.equals("Open")) {
            if (caller instanceof Gui1) {
                new Gui2();
            } else {
                new Gui1();
            }
            caller.dispose();
        }
    }
}
dly
  • 1,080
  • 1
  • 17
  • 23
0

Try only using one JFrame by making Gui1 and Gui2 panels. You can easily switch between them by getting rid of the first panel then adding the second.

      getContentPane().removeAll(); //gets rid of first panel
      getContentPane().add(panel); //adds desired panel to frame
      validate(); //updates frame with new panel