Hey everyone,

I am attempting to add an array of JTextFields to one of my JPanels and for some reason i keep getting errors and the program crashes when i run it. I know i am prob making some noob mistake but this a project for my first Java class so please do not look down on me lol

A picture of the INTENDED end result is here if u want to see it ManyNumbersStart

Any help would be greatly appreciated. Thank you!

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

public class ManyNumbers
{
    public static void main(String [] args)
    {
        ManyNumbersWindow mn = new ManyNumbersWindow("Kreg Khemmoro - Program 6");
    }
}

class ManyNumbersWindow extends JFrame
{
    public static final int MAX_NUM = 10;

    JTextField [] nums = new JTextField[MAX_NUM];



    public ManyNumbersWindow(String s)
    {
        super(s);

        //Begin JPanel for numbers textfields
        JPanel topGrid = new JPanel();
        topGrid.add(nums[0]);   

        JPanel botGrid = new JPanel();
        JPanel gridContainer = new JPanel(); 
        gridContainer.add(topGrid, BorderLayout.NORTH);
        gridContainer.add(botGrid, BorderLayout.SOUTH);


        //JPanel for buttons starts here
        JPanel southPanel = new JPanel();
        JButton btnGenNum = new JButton("New Numbers");
        JCheckBox bigNum = new JCheckBox("Big numbers");
        southPanel.setLayout(new FlowLayout());
        southPanel.add(btnGenNum);
        southPanel.add(bigNum);
        //Ends JPanel for buttons



        add(gridContainer, BorderLayout.NORTH);
        add(southPanel, BorderLayout.SOUTH);


        pack();
        setVisible(true);
        setResizable(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


}

what is the error message?

EDIT: if this is your first Java code, you're going at it wrong. you're trying to write Swing code, why you should start off with the basics.

hey stultuske, i fixed it. I created a for loop to run thru the array and initialize each of the textfields.

This is for a school assignment so I didnt have a choice. The prof started us out with this code and we are expected to fill it in to achieve the end result pictured above. I am not exactly sure how I am going to set all the text fields to 0 if you could help with that at all?

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

public class ManyNumbers
{
    public static void main(String [] args)
    {
        ManyNumbersWindow mn = new ManyNumbersWindow("Alan Jackson - Program 6");
    }
}

class ManyNumbersWindow extends JFrame
{
    public static final int MAX_NUM = 10;

    JTextField [] nums = new JTextField[MAX_NUM];



    public ManyNumbersWindow(String s)
    {
        super(s);
        setLayout(new FlowLayout());




        // You can use "pack" instead of "setSize" -- pack makes 
        // the window just the right size for the added components.
        pack();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


}

easier then you think. just call
.setText("0");

on all the instances. but the code you posted does not reflect your comment that you solved, let alone solved it using a for-loop.

Stultuske, thanks a million man. I did something very similar. Currently I am trying to convert the JTextFields into integers so that i can change the background color of the fields with a value higher than 50. For some reason i keep getting the error

"Not a statement"

when i write the code like this. Any suggestions? All of my code is there but the issue begins on line 103

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

public class ManyNumbers
{
    public static void main(String [] args)
    {
        ManyNumbersWindow mn = new ManyNumbersWindow("Kreg Khemmoro - Program 6");
    }
}

class ManyNumbersWindow extends JFrame
{
    public static final int MAX_NUM = 10;

    JTextField [] nums = new JTextField[MAX_NUM];

    JButton btnGenNum = new JButton("New Numbers");
    JCheckBox bigNum = new JCheckBox("Big numbers");

    public ManyNumbersWindow(String s)
    {
        super(s);

        //Begin JPanel for numbers textfields
        JPanel topGrid = new JPanel();
            for (int f = 0; f < 5; f++)
            {
                nums[f] = new JTextField(10);
                topGrid.add(nums[f]);   
            }   

        JPanel botGrid = new JPanel();
            for(int c = 5; c < 10; c++)
            {
                nums[c] = new JTextField(10);
                botGrid.add(nums[c]);
            }

        JPanel container = new JPanel();
        container.setLayout(new BorderLayout());
        container.add(topGrid, BorderLayout.NORTH);
        container.add(botGrid, BorderLayout.SOUTH); 
        //END JPanel for numbers textfields

        //Set values to 0 for textfields
        for(int counter = 0; counter < nums.length; counter++)
        {
            nums[counter].setText(" " + 0);
        }
        //END setting values to 0

        //Buttons and Event Handling Begin

        btnGenNum.addActionListener(new GenerateNumbers());
        bigNum.addActionListener(new BigNumbers());

        //END Buttons and Event Handling

        //JPanel for buttons starts here
        JPanel southPanel = new JPanel();
        southPanel.setLayout(new FlowLayout());
        southPanel.add(btnGenNum);
        southPanel.add(bigNum);
        //Ends JPanel for buttons


        //Main Layout Begins
        add(container, BorderLayout.NORTH);
        add(southPanel, BorderLayout.SOUTH);

        //END Main Layout


        pack();
        setVisible(true);
        setResizable(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    class GenerateNumbers implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            Random rand = new Random();

            for(int count = 0; count < nums.length; count++)
            {
                nums[count].setText(" " + rand.nextInt(100));
            }
        }
    }

    class BigNumbers implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            if  (bigNum.isSelected() == true)
            {
                for(int c = 0; c < nums.length; c++)
                    String s = nums[c].getText();
                    int parsedField = Integer.parseInt(s);

                }
            }
        }
    }

OK i am step further now. My goal is to highlight all the numbers >=50 but for some reason only a few fields will highlight and they will do it whether they are above 50 or now lol my code is below

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

public class ManyNumbers
{
    public static void main(String [] args)
    {
        ManyNumbersWindow mn = new ManyNumbersWindow("Kreg Khemmoro - Program 6");
    }
}

class ManyNumbersWindow extends JFrame
{
    public static final int MAX_NUM = 10;

    JTextField [] nums = new JTextField[MAX_NUM];

    JButton btnGenNum = new JButton("New Numbers");
    JCheckBox bigNum = new JCheckBox("Big numbers");

    public ManyNumbersWindow(String s)
    {
        super(s);

        //Begin JPanel for numbers textfields
        JPanel topGrid = new JPanel();
            for (int f = 0; f < 5; f++)
            {
                nums[f] = new JTextField(10);
                topGrid.add(nums[f]);   
            }   

        JPanel botGrid = new JPanel();
            for(int c = 5; c < 10; c++)
            {
                nums[c] = new JTextField(10);
                botGrid.add(nums[c]);
            }

        JPanel container = new JPanel();
        container.setLayout(new BorderLayout());
        container.add(topGrid, BorderLayout.NORTH);
        container.add(botGrid, BorderLayout.SOUTH); 
        //END JPanel for numbers textfields

        //Set values to 0 for textfields
        for(int counter = 0; counter < nums.length; counter++)
        {
            nums[counter].setText("0");
        }
        //END setting values to 0

        //Buttons and Event Handling Begin

        btnGenNum.addActionListener(new GenerateNumbers());
        bigNum.addActionListener(new BigNumbers());

        //END Buttons and Event Handling

        //JPanel for buttons starts here
        JPanel southPanel = new JPanel();
        southPanel.setLayout(new FlowLayout());
        southPanel.add(btnGenNum);
        southPanel.add(bigNum);
        //Ends JPanel for buttons


        //Main Layout Begins
        add(container, BorderLayout.NORTH);
        add(southPanel, BorderLayout.SOUTH);

        //END Main Layout


        pack();
        setVisible(true);
        setResizable(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    class GenerateNumbers implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            Random rand = new Random();

            for(int count = 0; count < nums.length; count++)
            {
                nums[count].setText("" + rand.nextInt(100));
            }
        }
    }

    class BigNumbers implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            if  (bigNum.isSelected() == true)
            {
                for(int count = 0; count < nums.length; count++)
                {
                    String text = nums[count].getText();
                    int currentField = Integer.parseInt(text);

                    if(currentField >= 50)
                    {
                        nums[count].setBackground(Color.RED);
                    }
                    else
                    {
                        nums[count].setBackground(Color.WHITE);
                    }
                }

            }
        }
    }
}

there are two ways to use

  • JTable instead of plain JTextField

  • JFormattedTextField with number formatter, instead of set / parsing number valur from plain JTextField

________________________________________________________________________________________________________________________________________________________

you can to use JTextField with DocumentFilter (filtering of uinwanted char(s)) for number value

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.