Search code examples
javaarraysstringjtextfield

JTextfield.getText() not returning any value


I am relatively new to java, so i'm not the best at it. In my program, I have a board with an Array of JTextfields in which a player enters values and when they press the button, if it is a valid input, the values will be stored in another array. This works perfectly for the RED block of code but for some reason, the JTextfield.getText() method does not return any value even if I have written something in them in the BLUE block of code. I have looked everywhere.

Panel Class:

package main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

public class Panel extends JPanel implements Runnable{
    
    
    public JLabel board = new JLabel();
    public JLabel rules = new JLabel();
    public JLabel redsetup = new JLabel("RED SETUP");
    public JLabel bluesetup = new JLabel("BLUE SETUP");
    public JLabel redinvalid = new JLabel("INVALID SETUP");
    public JLabel blueinvalid = new JLabel("INVALID SETUP");
    public JLabel graveyard = new JLabel();
    public JLabel brand = new JLabel();
    public JButton accept = new JButton("OK");
    public JButton acceptII = new JButton("OK");
    public JButton acceptIII = new JButton("OK");
    public JTextField[] redfield = new JTextField[40];
    public JTextField[] bluefield = new JTextField[40];
    public Font font = new Font("Cambria", Font.ITALIC, 28);
    public String turn;
    public String[] redval = new String[40];
    public String[] blueval = new String[40];
    public boolean startGame = false;
    public int posY = 144;
    public int posX = 41;
    public int redtwo = 8;
    public int redone = 1;
    public int redthree = 5;
    public int redfour = 4;
    public int redfive = 4;
    public int redsix = 4;
    public int redseven = 3;
    public int redeight = 2;
    public int rednine = 1;
    public int redten = 1;
    public int redbomb = 6;
    public int redflag = 1;
    public int blueflag =1;
    public int bluebomb = 6;
    public int blueone = 1;
    public int bluetwo = 8;
    public int bluethree = 5;
    public int bluefour = 4;
    public int bluefive = 4;
    public int bluesix = 4;
    public int blueseven = 3;
    public int blueeight = 2;
    public int bluenine = 1;
    public int blueten = 1;
    

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
public Panel() {
        
        this.setPreferredSize(new Dimension(320, 240));
        this.setBackground(Color.black);
        this.setDoubleBuffered(true);
        this.setFocusable(true);
        this.setLayout(null);
        
        for(int i =0; i<40; i++) {
            redfield[i] = new JTextField();
            redfield[i].setBounds(posX, posY, 24, 24);
            redfield[i].setBackground(Color.decode("#a67c52"));
            redfield[i].setForeground(Color.red);
            redfield[i].setBorder(new LineBorder(Color.decode("#77593b"), 1));
            redfield[i].setVisible(false);
            this.add(redfield[i]);
            if(posX == 257) {
                posX = 41;
                posY += 24;
            }else {
                posX+=24;
            }
        }
        
        
        redsetup.setBounds(90, 60, 200, 28);
        redsetup.setFont(font);
        redsetup.setForeground(Color.red);
        redsetup.setVisible(false);
        
        
        this.add(redsetup);
        
        bluesetup.setBounds(90, 60, 200, 28);
        bluesetup.setFont(font);
        bluesetup.setForeground(Color.blue);
        bluesetup.setVisible(false);
        
        
        this.add(bluesetup);
        
        redinvalid.setBounds(70, 10, 200, 28);
        redinvalid.setFont(font);
        redinvalid.setForeground(Color.RED);
        redinvalid.setVisible(false);
        
        
        this.add(redinvalid);
        
        
        blueinvalid.setBounds(70, 10, 200, 28);
        blueinvalid.setFont(font);
        blueinvalid.setForeground(Color.blue);
        blueinvalid.setVisible(false);
        
        
        this.add(blueinvalid);
        
        
        acceptII.setBounds(145,110,30,20);
        acceptII.setBackground(Color.red);
        acceptII.setForeground(Color.BLACK);
        acceptII.setBorder(new LineBorder(Color.BLACK, 2));
        acceptII.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                
                for(int i =0; i<40; i++) {
                    redval[i] = redfield[i].getText();
                    if(redval[i].equals("b") || redval[i].equals("B")) {
                        redbomb--;
                    }else if(redval[i].equals("f") || redval[i].equals("F") ){
                        redflag--;
                    }else if(redval[i].equals("1")){
                        redone--;
                    }else if(redval[i].equals("2")){
                        redtwo--;
                    }else if(redval[i].equals("3")){
                        redthree--;
                    }else if(redval[i].equals("4")){
                        redfour--;
                    }else if(redval[i].equals("5")){
                        redfive--;
                    }else if(redval[i].equals("6")){
                        redsix--;
                    }else if(redval[i].equals("7")){
                        redseven--;
                    }else if(redval[i].equals("8")){
                        redeight--;
                    }else if(redval[i].equals("9")){
                        rednine--;
                    }else if(redval[i].equals("10")){
                        redten--;
                    }
                    if(redbomb != 0 || redflag != 0 || redone != 0 || redtwo != 0 || redthree != 0 || redfour != 0 || redfive != 0 || redfive != 0 || redsix != 0 || redseven != 0 || redeight != 0 || rednine != 0 || redten != 0 ) {
                        redinvalid.setVisible(true);
                    }else {
                        redinvalid.setVisible(false);
                        acceptII.setVisible(false);
                        redsetup.setVisible(false);
                        for(int ii =0; ii<40; ii++) {
                             redfield[ii].setVisible(false);
                             blueSetUpTime();
                         }
                    }
                    
                    
            }
        }});
        this.add(acceptII);
        acceptII.setVisible(false);
        
        acceptIII.setBounds(145,110,30,20);
        acceptIII.setBackground(Color.blue);
        acceptIII.setForeground(Color.BLACK);
        acceptIII.setBorder(new LineBorder(Color.BLACK, 2));
        this.add(acceptIII);
        acceptIII.setVisible(false);
acceptIII.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                //copy 'verify setup' code
                blueVerification();
                
            }
        });
        
        
        ImageIcon bigboard = new ImageIcon("Board.png");
        board.setIcon(bigboard);
        board.setBounds(40, 0, 240,240);
        this.add(board);
        
        board.setVisible(false);
        
        ImageIcon grave = new ImageIcon("Graveyard.png");
        graveyard.setIcon(grave);
        graveyard.setBounds(0, 0, 40,240);
        this.add(graveyard);
        
        graveyard.setVisible(false);
        
        
        ImageIcon branding = new ImageIcon("Brand.png");
        brand.setIcon(branding);
        brand.setBounds(280, 0, 40,240);
        this.add(brand);
        
        brand.setVisible(false);
        
        accept.setBounds(290,0,30,20);
        accept.setBackground(Color.WHITE);
        accept.setForeground(Color.BLACK);
        accept.setBorder(new LineBorder(Color.BLACK, 2));
        accept.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                rules.setVisible(false);
                board.setVisible(true);
                accept.setVisible(false);
                redsetup.setVisible(true);
                acceptII.setVisible(true);
                graveyard.setVisible(true);
                brand.setVisible(true);
                start();
            }
        });
        this.add(accept);
        
        ImageIcon bigrules = new ImageIcon("Rules.png");
        rules.setIcon(bigrules);
        rules.setBounds(0, 0, 320,240);
        this.add(rules);
        
        rules.setVisible(true);
        
        
        
}
public void blueSetUpTime() {
    posY = 144;
    posX = 41;
    for(int i =0; i<40; i++) {
        
        bluefield[i] = new JTextField();
        bluefield[i].setBounds(posX, posY, 24, 24);
        bluefield[i].setBackground(Color.decode("#a67c52"));
        bluefield[i].setForeground(Color.blue);
        bluefield[i].setBorder(new LineBorder(Color.decode("#77593b"), 1));
        bluefield[i].setVisible(true);
        this.add(bluefield[i]);
        if(posX == 257) {
            posX = 41;
            posY += 24;
        }else {
            posX+=24;
        }
    }
    
    bluesetup.setVisible(true);
    acceptIII.setVisible(true);
    
}

public void blueVerification() {
    System.out.println("performing action");
    for(int i =0; i<40; i++) {
        System.out.println("for looping...");
        blueval[i] = bluefield[i].getText();
        if(blueval[i].equals("b") || blueval[i].equals("B")) {
            bluebomb--;
        }else if(blueval[i].equals("f") || blueval[i].equals("F") ){
            blueflag--;
        }else if(blueval[i].equals("1")){
            blueone--;
        }else if(blueval[i].equals("2")){
            bluetwo--;
        }else if(blueval[i].equals("3")){
            bluethree--;
        }else if(blueval[i].equals("4")){
            bluefour--;
        }else if(blueval[i].equals("5")){
            bluefive--;
        }else if(blueval[i].equals("6")){
            bluesix--;
        }else if(blueval[i].equals("7")){
            blueseven--;
        }else if(blueval[i].equals("8")){
            blueeight--;
        }else if(blueval[i].equals("9")){
            bluenine--;
        }else if(blueval[i].equals("10")){
            blueten--;
        }
        if(bluebomb != 0 || blueflag != 0 || blueone != 0 || bluetwo != 0 || bluethree != 0 || bluefour != 0 || bluefive != 0 || bluefive != 0 || bluesix != 0 || blueseven != 0 || blueeight != 0 || bluenine != 0 || blueten != 0 ) {
            blueinvalid.setVisible(true);
            System.out.println("invalid setup");
        }else{
            System.out.println("valid setup, clearing...");
            blueinvalid.setVisible(false);
            acceptIII.setVisible(false);
            bluesetup.setVisible(false);
            for(int ii =0; ii<40; ii++) {
                 bluefield[ii].setVisible(false);
                 
             }
            System.out.println("BEGIN GAME");
            
        }
        
        
}
    System.out.println(Arrays.toString(blueval));
}
    


public void start() {
     startGame = true;
     new Thread(this).start();
 }

public void run() {
    
 for(int i =0; i<40; i++) {
     redfield[i].setVisible(true);
 }

//System.exit(0);
}

}

When I ran it the red code worked fine but the blue code would always be "invalid setup"


Solution

  • So, your primary issue is here...

    for(int ii =0; ii<40; ii++) {
        redfield[ii].setVisible(false);
        blueSetUpTime();
    }
    

    You're calling blueSetUpTime 40 times, which is adding 1600 text fields to your screen ... good luck working out which one's are actually assigned to your array.

    There a numerous other issues. You really shouldn't be using null layouts, take the time to learn and use the layout managers. Remember, you're not stuck to a single layout manager and can make use of compounding containers with individual layouts based on your needs.

    I might consider possibly making use of a CardLayout or simply re-seeding the model when you switch between red and blue, but that's up to you.

    I'd also consider breaking your UI down into individual areas of responsibility. Since the red and blue setup is basically the same, that could be its own component and you could make use of data models to seperate the two. This is a concept commonly known as model-view-controller.

    I would consider initialising your components ahead of time so you don't run into an issue like this again.