Q:

Move from a frame to a frame in Swing

Unfortunately, our team leader has decided that we'll use Swing for this light desktop app. I have no previous experience in working with Swing. I will be working on the GUI side of the project. I've already created a frame with elements within it and wrote the logic in the event listeners. Now I want to redirect the user to a new frame after he's logged in.
How do I do that? Thanks
P.S. I'd appreciate if you could point me to a good tutorial for beginners

A:

Your frame should stay the same, just create and show new JPanel instead old one.
See below JPanel  painting process:

A:

As @Gilbert Le Blanc has pointed out (+1 to him). In Swing it is bad practice to use multiple JFrames.
To accomplish what you want:

Use CardLayout which allows dynamic switching of components
Or use JFrame#removeAll() and add a new JPanel (+1 Fess)
Try using JDialog/JOptionPane and then redirect to main JFrame
here is a good link on the topic: How to Make Dialogs

A:

It sounds like you have your terminology mixed up.  When we hear "Frame" we think JFrame which is equivalent to "Window".  So most of the time we'd recommend not using multiple windows, but changing the content of the window.  The content is generally made with a "JPanel".
So generally, you set up your JFrame, you set the content with this:
JPanel loginPanel = new JPanel();
frame.setContentPane(loginPanel);

If you want to replace your login panel with a new panel, just pass the new panel to that function:
JPanel mainMenuPanel = new JPanel();
frame.setContentPane(mainMenuPanel);

(of course you want some content in those panels)
Here's a simple example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PanelRetriever{

    Box panel1;
    JPanel panel2;

    public PanelRetriever(final JFrame frame){
        //Build the first login panel
        panel1 = Box.createVerticalBox();
        panel1.setBackground(Color.orange);
        panel1.setOpaque(true);
        panel1.add(Box.createVerticalGlue());
        panel1.add(new JTextField(10));
        JButton login = new JButton("Login");
        login.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                frame.setContentPane(getPanel2());
                frame.validate();
            }});
        panel1.add(login);
        panel1.add(Box.createVerticalGlue());

        //Build second panel
        panel2 = new JPanel();
        panel2.setBackground(Color.blue);
        panel2.setOpaque(true);
        JButton logout = new JButton("Logout");
        logout.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setContentPane(getPanel1());
                frame.validate();
            }});
        panel2.add(logout, BorderLayout.CENTER);
    }

    public Container getPanel1(){
        return panel1;
    }

    public Container getPanel2(){
        return panel2;
    }

    public static void main(String args[])
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame frame = new JFrame();
                PanelRetriever pr = new PanelRetriever(frame);
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                frame.setContentPane(pr.getPanel1());
                frame.setPreferredSize(new Dimension(500, 400));
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

