Learner
Learner

Reputation: 21405

How to create a swing application with multiple pages

Earlier I have created a small application using servlets where the first page shows a user registration page that captures basic information like user's first name and last name etc. with a Submit button.

Let's say the user first name entered in first page is "Scott" then once the user submits the form then in second page I have created a welcome message as "Welcome Scott". Then I provided an option to see list of users who have registered to the application earlier by connecting to database.

Now I wanted to implement the same in Swing application. I am new to Swings so I tried to learn it from various sources but I was not able to find an example where I can navigate between multiple frames/panels.

For the first page I have created below program that displays the text fields and a submit button:

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 UserRegistration extends JFrame {

    JButton button;

    public UserRegistration() {

        setSize(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        final JLabel label = new JLabel("Please enter details: ");
        JLabel firstName = new JLabel("First Name");
        JLabel lastName = new JLabel("Last Name");
        final JTextField firstNameTxt = new JTextField(20);
        final JTextField lastNameTxt = new JTextField(20);

        button = new JButton("Submit");
        JButton button1 = new JButton("Cancel");

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dispose();
                UserDetails user = new UserDetails();
                user.showDetails();
                user.setVisible(true);
            }
        });

        add(label);
        add(firstName);
        add(firstNameTxt);
        add(lastName);
        add(lastNameTxt);
        add(button);
        add(button1);

        setVisible(true);

    }
    public static void main(String args[]) {
        new UserRegistration();
    }

}

To go from 1st frame to 2nd frame I am using these lines:

                dispose();
                UserDetails user = new UserDetails();
                user.showDetails();
                user.setVisible(true);

Now for second page I am not able to find out how to get the parameters the user has entered, this is the code that I am stuck at:

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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

public class UserDetails extends JFrame {

    //private JFrame mainFrame;
    private JLabel headerLabel;
    private JLabel statusLabel;
    private JPanel controlPanel;

    public UserDetails() {
        prepareGUI();
    }


    private void prepareGUI() {
        //mainFrame = new JFrame("Java Swing Examples");
        setSize(800, 800);
        setLayout(new GridLayout(3, 5));
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });
        headerLabel = new JLabel("This is Header", JLabel.CENTER);
        statusLabel = new JLabel("This is Status", JLabel.CENTER);

        statusLabel.setSize(350, 100);

        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());

        System.out.println(controlPanel);
        add(headerLabel);
        add(controlPanel);
        add(statusLabel);
        //setVisible(true);
    }

    public void showDetails() {

        // Here I want display the welcome message and also to add all the registered user details.
    }
}

Please help me how to create an application that contains multiple pages?

Upvotes: 0

Views: 18062

Answers (1)

ZakiMak
ZakiMak

Reputation: 2102

Swing provides CardLayout class to provide what you need. It allows you to load two or more components(JPanels) and allowing to share the same display. Its possible to navigate between the cards just like a wizard. Please look into this tutorial:CardLayout

Upvotes: 5

Related Questions