trifectabihnocap
trifectabihnocap

Reputation: 3

How do you obtain 2 inputs from a user in Jtextfield?

I want the user to type a name for player one and press enter and then for the textfield to be blank again so that the user can type in a name for player 2. so far I can only get player one's name as input but not player two. For some reason my code isn't working. Any help would be greatly appreciated. Thanks in advance.

import java.lang.*;
import java.util.*;
import java.util.List;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

public class mainClass extends JPanel implements ActionListener {
    
    //constant variables to use

    static String playerOneName;
    static String playerTwoName;
    static boolean playerOneNameSet;
    static boolean playerTwoNameSet;
    
    
    private static final long serialVersionUID = 1L;
    
    public mainClass() {
        
    }
    
    public void paintComponent(Graphics g) {
        
        //set background color to white
        g.setColor(Color.white);
        g.fillRect(0, 0, getWidth(), getHeight());
        
      }

    public static void initializeBoard() {
    }
    public static void main(String[] args) {
        // title of frame
        JFrame frame = new JFrame("Risk");

            JTextField textField = new JTextField(20);
            frame.add(textField, BorderLayout.SOUTH);
            
            JLabel welcome = new JLabel("");
            welcome.setText("Please Enter name for Player 1 in the text box at the bottom");
            frame.add(welcome,BorderLayout.NORTH);
            
            //action listener listens for enter key
            textField.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    playerOneName= textField.getText();
                    System.out.println(playerOneName);
                    playerOneNameSet = true;
                    System.out.println(playerOneNameSet);
                }
            });
            if(playerOneNameSet == true) {
            JTextField textField2 = new JTextField(20);
            frame.add(textField2, BorderLayout.SOUTH);
            
            JLabel welcome2 = new JLabel("");
            welcome2.setText("Please Enter name for Player 2 in the text box at the bottom");
            frame.add(welcome2,BorderLayout.NORTH);
            
            //action listener listens for enter key
            textField2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    playerTwoName= textField2.getText();
                    System.out.println(playerTwoName);
                    playerTwoNameSet = true;
                    System.out.println(playerTwoNameSet);
                }
            });
            }
            
        
        
        // make sure it closes correctly
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //frame size in pixels
        final int FRAME_WIDTH = 1000;    
        final int FRAME_HEIGHT = 700;
        frame.setSize(FRAME_WIDTH,FRAME_HEIGHT);
        
        // makes sure the frame is visible
        frame.setVisible(true);
        mainClass main = new mainClass();
        frame.add(main);
        
        
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        
    }
}

Upvotes: 0

Views: 275

Answers (1)

Anuj
Anuj

Reputation: 1665

There is one logical error in your code.

If you want to use only one JTextField for two inputs, you do not need to create two JTextField. Just handle the ActionEvent and update only that JTextField. Here is the code for that:

    textField.addActionListener(new java.awt.event.ActionListener()
    {
        public void actionPerformed(java.awt.event.ActionEvent evt)
        {
            if (!playerOneNameSet)
            {
                playerOneName = textField.getText();
                textField.setText("");
                welcome.setText("Please Enter name for Player 2 in the text box at the 
                                 bottom");
                playerOneNameSet = true;
            }
            else
            {
                playerTwoName = textField.getText();
                textField.setText("");       
            }
         }
     });

The if part after that is not required. It will also eliminate the use of playerTwoNameSet.

If you want to use two JTextField, then you have to do it in the start properly without any logical flaw.

Logical Error in Detail:

Let me try to show you the flow of your program.

    public static void main(String[] args)
    {
        // title of frame
        JFrame frame = new JFrame("Risk");

        .
        .
        .
        .

        
        //action listener listens for enter key
        textField.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                playerOneName= textField.getText();
                System.out.println(playerOneName);
                playerOneNameSet = true;
                System.out.println(playerOneNameSet);
            }
        });

Your code is all good till here. After the above line, this happens

        if(playerOneNameSet == true)    //It is never executed

The reason this happens because playerOneNameSet is a static variable and its default value is false. This line is only executed once. Once your GUI is created, the main() method will not be called again until you run it again. After that, the control passes to the one JTextField that was created, that too when any ActionEvent is generated. It will never go to the if line after that.

I hope I have helped you. Do comment for any further problems.

Upvotes: 2

Related Questions