Search code examples
javaswinglayout-managerjdialogcardlayout

Redrawing a new JFrame in an existing one?


I'm trying to display a different JFrame after the user does something in the same window they are using, similar to a login feature. Haven't been able to figure out how to do that.

The workaround I have now is to just hide the current JFrame and then open a new one, which simulates a similar effect. But ideally I want it to just display the next JFrame in the same existing window.

import java.awt.FlowLayout;
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.JTextField;

public class Login extends JFrame {
    private static int x = 0;
    static JTextField txtInput = new JTextField(10);
    static JButton btnSwitch = new JButton("Log on");

    public Login(){
        setLayout(new FlowLayout());

        //add button and register
        add(new JLabel("Enter password:"));
        add(txtInput);
        add(btnSwitch);
    }

    public static void main(String[] args) {
        final JFrame frame = new Login();

        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 150);
        frame.setVisible(true);


        btnSwitch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(txtInput.getText().equals("123")){
                    //frame = new GUIHelloWorld(); this doesn't work because "The final local variable frame cannot be assigned, since it is defined in an enclosing type"
                    //so I went with the below workaround
                    GUIHelloWorld frame = new GUIHelloWorld();

                    frame.setLocationRelativeTo(null); // Center the frame
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setSize(300, 100);
                    frame.setVisible(true);
                }
            }
        });
    }
}

Once the user get pass the first part of the GUI, I want to show em something else like this:

import java.awt.GridLayout;

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

public class GUIHelloWorld extends JFrame {
    public GUIHelloWorld(){
        setLayout(new GridLayout(0,1));

        add(new JLabel("Hello World"));
        add(new JLabel("Welcome to the 2nd part of the GUI"));
    }

    public static void main(String[] args) {
        JFrame frame = new GUIHelloWorld();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 100);
        frame.setVisible(true);
    }

}

Can someone give me an idea of how to display a new JFrame into the existing window that the user is using?


Solution

    1. Don't extend from JFrame, especially in the case, frame's can't be added to other frames. Instead, based you individual UI views on something like JPanel
    2. Create a single instance of a JFrame, set it's layout manager to use a CardLayout.
    3. Add each of your view's to the frame, naming each view appropriately
    4. Use CardLayout to switch between the view as needed

    You could also consider using a JDialog for the login window, but the basic advice remains; create windows, extend components...