-1

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

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
Dragan
  • 3,713
  • 12
  • 42
  • 59

5 Answers5

4

Your frame should stay the same, just create and show new JPanel instead old one.

See below JPanel painting process:

enter image description here

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
4

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

Community
  • 1
  • 1
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
4

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);
            }
        });
    }
}
Nick Rippe
  • 6,465
  • 14
  • 30
  • can you give me complete simple example of what you told in the answer, i get your point above but could not succeed to implement it as i am beginner to swing.and i don't want to use multiple jframe in my gui application. – Mihir Jan 15 '13 at 17:22
  • @Mihir I added an example so you could see the code in action. – Nick Rippe Jan 16 '13 at 03:25
3

In Swing, you have one JFrame. You use JDialogs to get user id and password input from the user.

Here's a link to the Oracle Swing Tutorial.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
1

You can just create a new frame in much the same way as you already have;

MyFrame f = new MyFrame(); //MyFrame extends javax.swing.JFrame

And then bring it to the front with;

f.setVisible(true);
f.toFront();
lynks
  • 5,599
  • 6
  • 23
  • 42
  • but I dont want to dynamically create the frame in code. I will create it through the designer.. – Dragan Oct 17 '12 at 12:50
  • A new `JFrame` will create a new *frame*, that is a new 'window' on your desktop, leaving the old one where it is. If you just want to change the content, then swap out a new `JPanel`. – lynks Oct 17 '12 at 12:52
  • 2
    its bad practice to use multiple JFrames: Bad (bad, bad) practice. User unfriendly: The user sees multiple icons in their task bar when expecting to see only one. Plus the side effects of the coding problems.. A nightmare to code and maintain: A modal dialog offers the easy opportunity to focus attention on the content of that dialog - choose/fix/cancel this, then proceed. Multiple frames do not. A dialog (or floating tool-bar) with a parent will come to front when the parent is clicked on - you'd have to implement that in frames if that was the desired behavior. – Maxim Shoustin Oct 17 '12 at 12:55
  • bad practice, maybe. bad user experience? that really depends, it might be that you want to give the impression of starting a new application without having to do IPC. either way, the question was how to switch from one frame to a new frame, which is what my answer provides, in its most simple form. – lynks Oct 17 '12 at 14:51
  • @Fess Gee that sounds familiar. ;) – Andrew Thompson Oct 17 '12 at 14:54
  • I think @Fess makes a good point (& yes, I am biased), this is so often a "How can I shoot myself in the foot?" question that it pays to be absolutely sure the use-case justifies it before explaining the how. I have yet to find a situation that does justify it. – Andrew Thompson Oct 17 '12 at 14:57