Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7622727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:31:31+00:00 2026-05-31T04:31:31+00:00

I’m trying to display user name on the next card after user logged in,

  • 0

I’m trying to display user name on the next card after user logged in, and having no luck.

I’m using CardLayout and have defined two cards – one card for user to input name & password and 2nd to display welcome message with logged in uner name. I’m learing Java & Swing my own and not the expert. Any help at all, including fixing this code or references for me to go read about, would be greatly appreciated.

Here is my current code (still need to add code to update text field of welcome screen):

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CardTest
{   
    private JFrame frame;
    public static final String CARD_LOGIN =  "Card Login"; 
    public static final String CARD_DEPARTMENT = "Card Department";
    public static final String CARD_TEAM = "Card Team";
    public static JPanel cards;
    public Employee employee = null;
    public CardLogin cardLogin = null;
    public CardDepartment cardDepartment = null;
    public CardTeam cardTeam = null;

    public CardTest()
    {
        Employee employee = new Employee();

        frame = new JFrame("Card Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);

        cards = new JPanel();
        cards.setLayout(new CardLayout(20, 20));

        cardLogin = new CardLogin(this, employee);
        cardDepartment = new CardDepartment(this, employee);
        cardTeam = new CardTeam(this, employee);

        cards.add(cardLogin, CARD_LOGIN);       
        cards.add(cardDepartment, CARD_DEPARTMENT);
        cards.add(cardTeam, CARD_TEAM);

        frame.getContentPane().add(cards);
        frame.pack();
        frame.setVisible(true);
    }

    public void swapView(String key) 
    {
        CardLayout cardLayout = (CardLayout) cards.getLayout();
        cardLayout.show(cards, key);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new CardTest();
            }
        });
    }
}

class CardLogin extends JPanel 
{ 
    private ActionListener action; 
    private JTextField tfUsername= null; 
    Employee employee;
    CardTest cardTest;

    public CardLogin(CardTest cardTest, Employee employee) 
    { 
        this.cardTest = cardTest;
        this.employee = employee;
        init(); 
    } 

    private void init() 
    { 
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();

        gc.fill = GridBagConstraints.HORIZONTAL;

        JLabel lbCardName = new JLabel("Login Card ");
        gc.gridx = 1;
        gc.gridy = 0;
        gc.gridwidth = 2;
        panel.add(lbCardName, gc);

        JLabel lbUsername = new JLabel("Username: ");
        gc.gridx = 0;
        gc.gridy = 2;
        gc.gridwidth = 1;
        panel.add(lbUsername, gc);

        tfUsername = new JTextField(20);
        gc.gridx = 1;
        gc.gridy = 2;
        gc.gridwidth = 2;
        panel.add(tfUsername, gc);

        JLabel lbPassword = new JLabel("Password: ");
        gc.gridx = 0;
        gc.gridy = 3;
        gc.gridwidth = 1;
        panel.add(lbPassword, gc);

        JPasswordField pfPassword = new JPasswordField(20);
        gc.gridx = 1;
        gc.gridy = 3;
        gc.gridwidth = 2;
        panel.add(pfPassword, gc);

        final JButton loginButton = new JButton("Login"); 

        action = new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            {
                if (tfUsername.getDocument().getLength() > 0)   
                {
                    employee.setUserName(tfUsername.getText());
                    cardTest.swapView(cardTest.CARD_DEPARTMENT);
                }
            } 
        }; 

        loginButton.addActionListener(action); 

        JPanel bp = new JPanel();
        bp.add(loginButton);

        setSize( 640, 480);

        add(panel, BorderLayout.CENTER);
        add(bp, BorderLayout.PAGE_END);
    } 
} 

class CardDepartment extends JPanel 
{ 
    private ActionListener actionNext; 
    private ActionListener actionLogout; 
    private JTextField tfDepartment= null; 
    private String department= null;
    Employee employee;
    CardTest cardTest;
    CardLogin cardLogin;

    public CardDepartment(CardTest cardTest, Employee employee) 
    { 
        this.employee = employee;
        this.cardTest = cardTest;
        init(); 
    } 

    private void init() 
    { 
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();
        gc.fill = GridBagConstraints.HORIZONTAL;

        JLabel lbCardName = new JLabel("Department Card ");
        gc.gridx = 1;
        gc.gridy = 0;
        gc.gridwidth = 2;
        panel.add(lbCardName, gc);

        JLabel lbWelcome = new JLabel("Welcome ");
        gc.gridx = 0;
        gc.gridy = 2;
        gc.gridwidth = 1;
        panel.add(lbWelcome, gc);

        gc.gridx = 1;
        gc.gridy = 2;
        gc.gridwidth = 2;
        panel.add(new JLabel(employee.getUserName()), gc);

        JLabel lbDepartment = new JLabel("Enter Department: ");
        gc.gridx = 0;
        gc.gridy = 3;
        gc.gridwidth = 1;
        panel.add(lbDepartment, gc);

        tfDepartment = new JTextField(20);
        gc.gridx = 1;
        gc.gridy = 3;
        gc.gridwidth = 2;
        panel.add(tfDepartment, gc);

        final JButton nextButton = new JButton("Next"); 
        actionNext = new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            {
                if (tfDepartment.getDocument().getLength() > 0)   
                {
                    department = tfDepartment.getText();
                    cardTest.swapView(cardTest.CARD_TEAM);
                }
            } 
        }; 

        final JButton logoutButton = new JButton("Logout"); 
        actionLogout = new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            {
                    cardTest.swapView(cardTest.CARD_LOGIN);
            } 
        }; 

        nextButton.addActionListener(actionNext); 
        logoutButton.addActionListener(actionLogout); 

        JPanel bp = new JPanel();
        bp.add(panel);
        bp.add(logoutButton);
        bp.add(nextButton);

        add(panel);
        add(bp); 
    } 
}

class CardTeam extends JPanel 
{ 
    private ActionListener actionPrev; 
    private ActionListener actionLogout; 
    private JTextField tfTeam= null; 
    Employee employee;
    CardTest cardTest;

    public CardTeam(CardTest cardTest, Employee employee) 
    { 
        this.cardTest = cardTest;
        this.employee = employee;
        init(); 
    } 

    private void init() 
    { 
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gc = new GridBagConstraints();
        gc.fill = GridBagConstraints.HORIZONTAL;

        JLabel lbCardName = new JLabel("Team Card ");
        gc.gridx = 1;
        gc.gridy = 0;
        gc.gridwidth = 2;
        panel.add(lbCardName, gc);

        JLabel lbWelcome = new JLabel("Welcome ");
        gc.gridx = 0;
        gc.gridy = 2;
        gc.gridwidth = 1;
        panel.add(lbWelcome, gc);

        gc.gridx = 1;
        gc.gridy = 2;
        gc.gridwidth = 2;
        panel.add(new JLabel(employee.getUserName()), gc);

        JLabel lbDepartment = new JLabel("Department: ");
        gc.gridx = 0;
        gc.gridy = 3;
        gc.gridwidth = 1;
        panel.add(lbDepartment, gc);

        gc.gridx = 1;
        gc.gridy = 3;
        gc.gridwidth = 2;
        panel.add(new JLabel(employee.getDepartment()), gc);

        JLabel lbTeam = new JLabel("Enter Team: ");
        gc.gridx = 0;
        gc.gridy = 4;
        gc.gridwidth = 1;
        panel.add(lbTeam, gc);

        tfTeam = new JTextField(20);
        gc.gridx = 1;
        gc.gridy = 4;
        gc.gridwidth = 2;
        panel.add(tfTeam, gc);

        final JButton prevButton = new JButton("Prev"); 
        actionPrev = new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            {
                    cardTest.swapView(cardTest.CARD_DEPARTMENT);
            } 
        }; 

        final JButton logoutButton = new JButton("Logout"); 
        actionLogout = new ActionListener() 
        { 
            public void actionPerformed(ActionEvent ae) 
            {
                    cardTest.swapView(cardTest.CARD_LOGIN);
            } 
        }; 

        prevButton.addActionListener(actionPrev); 
        logoutButton.addActionListener(actionLogout); 

        JPanel bp = new JPanel();
        bp.add(logoutButton);
        bp.add(prevButton);

        add(panel);
        add(bp); 
    } 
}

class Employee
{ 
    private String userName = null;
    private String department = null;
    private String team = null;

    public Employee() {
        super();
    }

    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    public String getTeam() {
        return team;
    }
    public void setTeam(String team) {
        this.team = team;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:31:32+00:00Added an answer on May 31, 2026 at 4:31 am

    You are declaring and initializing JPanel cards twice in your code, once as an Instance variable and the second time inside the constructor of CardLayoutLoginTest class.
    As yours is a step by step thingy, so it’s better you add your JPanel which are acting as Cards, one by one to the CardLayout. Since if the LoginFails you won’t need them, just when login is valid add what ever you want to add to the CardLayout.

    Do use setLocationByPlatform(true); instead of setLocationRelativeTo(null);. Former is better as explained by @Andrew Thompson in one of his posts, How to best position Swing GUI’s
    I had modified your code, do have a look and let me know if it’s short of something.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class CardTest
    {   
        private JFrame frame;
        public static final String CARD_LOGIN =  "Card Login"; 
        public static final String CARD_WELCOME = "Card Welcome";
        public static JPanel cards;
        public CardLogin cardLogin = null;
    
        public CardTest()
        {
            frame = new JFrame("Card LOGIN");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationByPlatform(true);
    
            cards = new JPanel();
            cards.setLayout(new CardLayout(20, 20));
    
            cardLogin = new CardLogin(this);
            cards.add(cardLogin, CARD_LOGIN);       
    
            frame.getContentPane().add(cards);
            frame.pack();
            frame.setVisible(true);
        }
    
        public void swapView(String key) 
        {
            CardLayout cardLayout = (CardLayout) cards.getLayout();
            cardLayout.show(cards, key);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new CardTest();
                }
            });
        }
    }
    
    class CardLogin extends JPanel 
    { 
        private ActionListener action; 
        CardTest cardLayoutLoginTest;
        /*
         *  Made JTextField an instance variable so that
         * ActionListener can access it or you can make 
         * it final.
         */
        private JTextField tfUsername= null; 
        private String username = null;
    
        public CardLogin(CardTest cardLayoutLoginTest) 
        { 
            this.cardLayoutLoginTest = cardLayoutLoginTest;
            init(); 
        } 
    
        private void init() 
        { 
    
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gc = new GridBagConstraints();
    
            gc.fill = GridBagConstraints.HORIZONTAL;
    
            JLabel lbUsername = new JLabel("Username: ");
            gc.gridx = 0;
            gc.gridy = 0;
            gc.gridwidth = 1;
            panel.add(lbUsername, gc);
    
            tfUsername = new JTextField(20);
            gc.gridx = 1;
            gc.gridy = 0;
            gc.gridwidth = 2;
            panel.add(tfUsername, gc);
    
            JLabel lbPassword = new JLabel("Password: ");
            gc.gridx = 0;
            gc.gridy = 1;
            gc.gridwidth = 1;
            panel.add(lbPassword, gc);
    
            JPasswordField pfPassword = new JPasswordField(20);
            gc.gridx = 1;
            gc.gridy = 1;
            gc.gridwidth = 2;
            panel.add(pfPassword, gc);
    
            final JButton loginButton = new JButton("Login"); 
    
            action = new ActionListener() 
            { 
                public void actionPerformed(ActionEvent ae) 
                {
    
                        // Here need code to update text filed of welcome card
                    /*
                     * Here we are first checking if there is any text inside
                     * the JTextField for USERNAME, if found we will send it to the
                     * next JPanel which will be serving as a new Card.
                     */ 
                    if (tfUsername.getDocument().getLength() > 0)   
                    {
                        username = tfUsername.getText();
                        CardWelcome cardWelcome = new CardWelcome(cardLayoutLoginTest.cardLogin);
                        CardTest.cards.add(cardWelcome, cardLayoutLoginTest.CARD_WELCOME);
                        cardLayoutLoginTest.swapView(cardLayoutLoginTest.CARD_WELCOME);
                    }
                } 
            }; 
    
            loginButton.addActionListener(action); 
    
            JPanel bp = new JPanel();
            bp.add(loginButton);
    
            /*set size of the frame*/
            setSize( 640, 480);
    
            add(panel, BorderLayout.CENTER);
            add(bp, BorderLayout.PAGE_END);
    
        } 
    
        public String getUserName()
        {
            return username;
        }
    } 
    
    class CardWelcome extends JPanel 
    { 
        private JTextField textField;
        private CardLogin cardLogin;
    
        public CardWelcome(CardLogin cl) 
        { 
            cardLogin = cl;
            init(); 
        } 
    
        private void init() 
        { 
            setLayout(new GridLayout(1, 1)); 
            JLabel userLabel = new JLabel("Welcome "); 
            textField = new JTextField(); 
            textField.setText(cardLogin.getUserName());
            System.out.println("UserName : " + cardLogin.getUserName());
    
            add(userLabel); 
            add(textField); 
        } 
    }
    

    A small sample program to update JLabel at runtime :

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class UpdateLabel extends JFrame
    {
        private int count = 0;
        public UpdateLabel()
        {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationByPlatform(true);
    
            final JPanel contentPane = new JPanel();
            contentPane.setLayout(new BorderLayout());
    
            final JLabel label = new JLabel("JLabel " + count);
            JButton button = new JButton("UPDATE JLABEL");
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    count++;
                    label.setText("JLabel " + count);
                    contentPane.revalidate(); // sometimes you require to do this and the below line.
                    contentPane.repaint();
                }
            });
    
            contentPane.add(label, BorderLayout.CENTER);
            contentPane.add(button, BorderLayout.PAGE_END);
    
            setContentPane(contentPane);
            pack();
            setVisible(true);
        }
    
        public static void main(String... args)
        {
            Runnable runnable = new Runnable()
            {
                public void run()
                {
                    new UpdateLabel();
                }
            };
            SwingUtilities.invokeLater(runnable);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.