Search code examples
swinguser-interfaceawtjlabelcardlayout

How to set label text to JTextField input


I'm trying to get a user to input a name in one panel on CardLayout and for the input to then define a JLabels text in the next panel that it switches to. I used System.out.println(userName); to double check that it was picking up the text and it definitely is but I don't know how to get the 'user' text to be that of the JTextFields input.

Full working code below:

import java.awt.CardLayout;
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.JPanel;
import javax.swing.JTextField;

public class gameDataPanel {

    private JFrame frame = new JFrame("Game Data Input");
    private JPanel mainPanel, loginPanel, gameDataPanel;
    private JLabel playerName, user;
    private JTextField playerNameInput;
    private JButton submit;
    private CardLayout gameDataLayout = new CardLayout();
    private String userName;

    public gameDataPanel() {

        mainPanel = new JPanel();

        mainPanel.setLayout(gameDataLayout);

        playerName = new JLabel("Enter Player Name: ");
        playerNameInput = new JTextField(30);
        submit = new JButton("Submit");

        loginPanel = new JPanel();
        loginPanel.add(playerName);
        loginPanel.add(playerNameInput);
        loginPanel.add(submit);

        gameDataPanel = new JPanel();
        user = new JLabel();
        user.setText(userName);
        gameDataPanel.add(user);

        mainPanel.add(loginPanel, "1");
        mainPanel.add(gameDataPanel, "2");
        gameDataLayout.show(mainPanel, "1");

        submit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                userName = playerNameInput.getText();
                gameDataLayout.show(mainPanel, "2");
                System.out.println(userName);
            }
        });

        playerNameInput.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {

                userName = playerNameInput.getText();
                gameDataLayout.show(mainPanel, "2");
                System.out.println(userName);
            }
        });

        frame.add(mainPanel);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
    }

    public static void main(String[] args) {
        gameDataPanel gd = new gameDataPanel();
    }

}

Thanks


Solution

  • I am not 100% sure, whether i understood you correctly: You want to display the users name, which is entered in "playerNameInput" to be displayed in the JLabel "user" ? In this case, you have to update your JLabel object in your event listener AFTER the user entered something and pressed the push button. Try something like this:

    playerNameInput.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
    
            userName = playerNameInput.getText();
            user.setText(userName);
            gameDataLayout.show(mainPanel, "2");
            System.out.println(userName);
        }
    });
    

    I hope this helps :-)