'How to go back to the previous GUI when a button is clicked in Java
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);
}
}
Solution 1:[1]
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);
}
}
}
Solution 2:[2]
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.
Solution 3:[3]
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();
}
}
}
Solution 4:[4]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharman |
| Solution 2 | Sw4Tish |
| Solution 3 | dly |
| Solution 4 | kama_kazzi |
