2

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;
    }
}
  • @AtifHameed : Please do check this [CardLayoutCode](http://pastebin.com/bc2WHRig), that I had modified a bit, that is one way to update things at a later stage. Please do provide me the code or project folder so I can have a look at the project as a whole. – nIcE cOw Mar 09 '12 at 06:02

2 Answers2

4

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);
    }
}
Community
  • 1
  • 1
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • @Arif Hameed here is answer to your question, please don't forget accept this answer – mKorbel Mar 06 '12 at 18:21
  • @Gagandeep Bali, thanks for the prompt respone and for the comments on duplicate declaration. I'm afraid this solution does not fit in real project as I have 7 cards added to the deck at the time of GUI creation and user can switch to any card and some data will be passed form active card to switched card and even on logoff from any card will display login card again. This small program I have created to test the functionality to pass values from one card to another.How do I handle CardLayout multiple instance creation situation in this example as commented by @mKorbel? – Arif Hameed Mar 07 '12 at 12:59
  • @ArifHameed : See instead of passing the value to the next Card, instead, this is what you must do. What ever you are entering, that input can basically relate to something (For Information : a Student, an Employee, a Worker or something), so what you can do is to make a separate class for this entity. So as you move from login to welcome window save the username and password for the particular entity in a respective class variables. – nIcE cOw Mar 07 '12 at 13:11
  • continued... Now you can simply pass the Objects of this class to different cards instead of passing each card's instance to another card. And for bringing yourself back to the Login Card once user Logout , you can check [CardLayout's first method](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html#first(java.awt.Container)) for going back to the first card. – nIcE cOw Mar 07 '12 at 13:11
  • Moreover, just to access one single method `swapView(...)` inside the `CardLayoutLoginTest`, you are passing the object, when you had made your `JPanel` carrying the CardLayout, as static, why not you simply use it in other classes to get to the `CardLayout's` object – nIcE cOw Mar 07 '12 at 13:15
  • @Gagandeep Bali: I followed your instructions and created a Employee class to hold data and the Object of this class I'm passing to different cards, still can't make it work. Now I have extedned example by adding two more cards to look like exactly my real project. Could you please take a look and let me know what am I doing wrong? I have replaced my above example with the updated one. – Arif Hameed Mar 08 '12 at 16:02
  • continued... one more thing, I have noticed you were updated value on a card before adding it to deck whereas I need to add all cards first to deck before I update any card! – Arif Hameed Mar 08 '12 at 16:10
  • @ArifHameed : Exactly, don't know seems like i got the vibes, I am also thinking on this issue, as to how to make it work for you. I tried making a Student class around 4 hours back. But since you are adding them in the start, so the changes cann't be made to the already initialized object. So I am also thinking about how to make it work, though I figured out that you dont' need to pass this to your constructors of `JPanel` so that you can use `swapView(...)` method. – nIcE cOw Mar 08 '12 at 17:27
  • @ArifHameed : If you won't mind may you post whole of your code at [PASTEBIN](http://pastebin.com/), I will surely have a look at it and will revert back. – nIcE cOw Mar 08 '12 at 17:33
  • @Gagandeep Bali: Brillent - this is exactly what I want for my project and it is working now! Many thanks for your help. For some reason JLabel update didn't work and I had to change all JLabel fields to JText. Can you please explain? – Arif Hameed Mar 13 '12 at 17:12
  • @ArifHameed : Happy to know, that I was of some help somewhere in your endeavour. Your Welcome and Keep Smiling :-) Are you doing those updates on `EDT`, and after making the changes, are you performing `panelObject.revalidate()` and `panelObject.repaint()`, for changes to take effect, when using `JLabel`. – nIcE cOw Mar 13 '12 at 17:15
  • @Gagandeep Bali: BTW, how do I accept your answer? Can't find any button or link to click? – Arif Hameed Mar 13 '12 at 17:17
  • Do read this for that, [How Accepting an Answer works](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). Shall i make you a small program to demonstrate how to update `JLabel` at runtime :-) – nIcE cOw Mar 13 '12 at 17:20
  • @Gagandeep Bali: Yes update on EDT, and was performaing panelObject.revalidate() but not panelObject.repaint(). I guess that is reason changes were not taking effect! – Arif Hameed Mar 13 '12 at 17:26
  • @ArifHameed : A small sample program i am adding, but if you are changing only the text, than mostly without using `revalidate()` and `repaint()` it works. Moreover if you are doing this inside some `ActionListener`'s `actionPerformed(...) method, then it's automatically on EDT.`Seems like I have to see the code to know the actual reason for JLabel thingy :-) – nIcE cOw Mar 13 '12 at 17:31
  • @Gagandeep Bali: That would be useful but now I ran into a bigger problem - one of Card I have a JTable with 3 columns and 10 rows. In first column each cell has a combobox which are link to 10 different arrays of item desc and amount. When 1st combo is selected, selected value should be displayed in 2nd column and the runing total should be build in 3rd column. This rule will apply to all combos and on random combo selection running total will be recalculated. Any suggestions, please? – Arif Hameed Mar 13 '12 at 17:41
  • @ArifHameed : I am really sorry about `JTable` part, never used them, :( . But I can try though, but for that I need to see your code, what you doing, and what exactly to be done to rectify the changes. I have to read the Tutorials for the `JTable` for that, so I do need the code for this. Never touched JTables before :( – nIcE cOw Mar 13 '12 at 17:47
  • @Gagandeep Bali: No problem with sharing code but all combos are papulated for SQL database and to replicate development envirnoment would not be straight forward. Tomorrow I'll see if I could created a simplified version of this card class and will post here... – Arif Hameed Mar 13 '12 at 18:03
  • @ArifHameed : IF you be making a simplified version, why not ask a new question ? That way you will get many helping hands to work on the same problem. – nIcE cOw Mar 13 '12 at 18:07
  • @Gagandeep Bali: okay, will do. – Arif Hameed Mar 14 '12 at 09:33
  • @Gagandeep Bali: Here is my new question posted http://stackoverflow.com/questions/9706878/calculating-running-totals-in-jtable-using-jcombobox can you please take a look? Thanks. – Arif Hameed Mar 14 '12 at 17:23
  • @ArifHameed : Sure, I be glad to help you on that :-) – nIcE cOw Mar 14 '12 at 17:40
3

you re_create CardLayout instance every times when JButton's Action fired, create that only once times, for example

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

public class OnTheFlyImageTest extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel cardPanel;
    private CardLayout cardLayout;

    public OnTheFlyImageTest() {
        JPanel cp = new JPanel(new BorderLayout());
        cp.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        cardLayout = new CardLayout(5, 5);
        cardPanel = new JPanel(cardLayout);
        cp.add(cardPanel);
        for (int i = 0; i < 5; i++) {// Create random panels for testing.
            String name = "ImagePanel" + (i + 1);
            String image = (i & 1) == 0 ? "foo.gif" : "bar.gif";
            Color clr = (i & 1) == 0 ? Color.red : Color.blue;
            ImagePanel imgPanel = new ImagePanel(name, image, clr);
            cardPanel.add(imgPanel, name);
            cardLayout.addLayoutComponent(imgPanel, name);
        }
        JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 5, 5));
        JButton prevButton = new JButton("< Previous");
        prevButton.setActionCommand("Previous");
        prevButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.previous(cardPanel);
            }
        });
        buttonPanel.add(prevButton);
        JButton nextButton = new JButton("Next >");
        nextButton.setActionCommand("Next");
        nextButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.next(cardPanel);
            }
        });
        buttonPanel.add(nextButton);
        JPanel temp = new JPanel(new BorderLayout());
        temp.add(buttonPanel, BorderLayout.LINE_END);
        cp.add(temp, BorderLayout.SOUTH);
        setContentPane(cp);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Test");
        pack();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new OnTheFlyImageTest().setVisible(true);
            }
        });
    }
}

class ImagePanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private String imgString;
    private JLabel imgLabel;

    public ImagePanel(String name, String imgString, Color backGround) {
        setName(name);
        this.imgString = imgString;
        setLayout(new BorderLayout());
        setBackground((backGround));
        // Ensure size is correct even before any image is loaded.
        setPreferredSize(new Dimension(400, 300));
    }

    @Override
    public void setVisible(boolean visible) {
        if (visible) {
            System.err.println(getName() + ": Loading and adding image");
            ImageIcon icon = new ImageIcon(imgString);
            imgLabel = new JLabel(icon);
            add(imgLabel);
        }
        super.setVisible(visible);
        if (!visible) { // Do after super.setVisible() so image doesn't "disappear".
            System.err.println(getName() + ": Removing image");
            if (imgLabel != null) { // Before display, this will be null
                remove(imgLabel);
                imgLabel = null; // Hint to GC that component/image can be collected.
            }
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319