Results 1 to 10 of 10
  1. #1
    hornet7288 is offline Member
    Join Date
    Sep 2016
    Posts
    32
    Rep Power
    0

    Default array of TextFields issue

    I am attempting to create a two dimensional (square matrix) array in a 10x10 format. After creating the array, I need to set each element equal to either 0 or 1. I am having trouble with the program failing whenever I try to add anything to the array. I am also trying to add the text fields to a gridpane using the same for loop. Any help would be appreciated! I am only including the part that I need help with. I do not think the rest of the code is relevant right now.

    Java Code:
    public class Exercise extends Application {
    
        public TextField[][] tf = new TextField[10][10];
        public int[][] numbers = new int[10][10];
    
        @Override
        public void start(Stage primaryStage) {
            
            GridPane gp0 = new GridPane();
    
            //Create 100 text boxes filled with either 0 or 1
            for (int row = 0; row < 10; row++) {
                for (int column = 0; column < 10; column++) {
    
                    numbers[row][column] = (int) (Math.random() * 2);
                    
                    gp0.add(tf[row][column], column, row);
                    
    
                }
            }
    Last edited by hornet7288; 09-13-2016 at 04:31 PM.

  2. #2
    hornet7288 is offline Member
    Join Date
    Sep 2016
    Posts
    32
    Rep Power
    0

    Default Re: array of TextFields issue

    Basically I am writing a program that initially displays a 10X10 matrix on a GUI. There are buttons to refresh the fields (giving them new integer values of 1 or 0 randomly) and a button to find the largest square of 1's. I am assuming I have to use all text fields because the user has to be able to change the zeros and ones. Not too sure the best way to solve this problem. I don't want someone to solve it completely for me, but I definitely need help with what I posted above. Not sure if I am even going about it the right way.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: array of TextFields issue

    You have an array (10x10) that can store that many TextFields, but they don't contain TextFields yet (i.e. they are all null); you have to create a TextField for each slot in the array. In your nested loop you can do:

    Java Code:
    tf[row]]column]= new TexrField();
    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: array of TextFields issue

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    hornet7288 is offline Member
    Join Date
    Sep 2016
    Posts
    32
    Rep Power
    0

    Default Re: array of TextFields issue

    Here I am trying to create 100 text fields and fill them with 100 integers (which were converted from int to string). I figured writing a small program to do this would be better for the purposes of figuring out how to get it to work versus messing with a larger program. It still does not seem to work. When I comment out the line in the nested loop where the textfields are assigned values, it seems to run without a problem. When I remove the comment the program does not run without error.

    Java Code:
    public class Test extends Application {
    
        @Override
        public void start(Stage primaryStage) {
    
            TextField[][] tfNums = new TextField[10][10];
            int[][] nums = new int[10][10];
    
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 10; j++) {
                    nums[i][j] = (int) (Math.random() * 10);
                    String number = Integer.toString(nums[i][j]);
    
                    tfNums[i][j].setText(number);
    
                }
            }
    
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: array of TextFields issue

    Did you read Jos' comment? Your array declaration just allocates space for the text fields. You need to create
    an instance of each text field and add it to the array. Then you add the text to the field. I bet your are probably
    getting a null pointer exception because the default values for the array are probably null.

    BTW, when you get this working, why create an array of nums? Just at the value to the text field as it is calculated.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    hornet7288 is offline Member
    Join Date
    Sep 2016
    Posts
    32
    Rep Power
    0

    Default Re: array of TextFields issue

    I did see Jos' comment and I tried to get it working but was not able to.

  8. #8
    hornet7288 is offline Member
    Join Date
    Sep 2016
    Posts
    32
    Rep Power
    0

    Default Re: array of TextFields issue

    Once it is working, like you mentioned I will probably eliminate the array of numbers and instead add a random value to the text field during each loop

  9. #9
    hornet7288 is offline Member
    Join Date
    Sep 2016
    Posts
    32
    Rep Power
    0

    Default Re: array of TextFields issue

    I got it working...disregard my last comment. The advice that Jos' said worked. I was just going about it the wrong way. Now I am going to clean it up some and apply it back to the larger code for my project.

    Java Code:
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.GridPane;
    import javafx.stage.Stage;
    
    public class Test extends Application {
    
        @Override
        public void start(Stage primaryStage) {
    
            TextField[][] tfNums = new TextField[10][10];
            int[][] nums = new int[10][10];
    
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 10; j++) {
                    tfNums[i][j] = new TextField();
                }
            }
    
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 10; j++) {
                    nums[i][j] = (int) (Math.random() * 10);
                    String number = Integer.toString(nums[i][j]);
    
                    tfNums[i][j].setText(number);
    
                }
            }
    
            GridPane gp0 = new GridPane();
    
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 10; j++) {
                    
                    gp0.add(tfNums[i][j], i, j);
                }
            }
            
            Scene scene = new Scene(gp0);
            
            primaryStage.setTitle("Test");
            primaryStage.setScene(scene);
            primaryStage.show();
            
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: array of TextFields issue

    If you look at your two loops they are pretty similar.
    You could do it all in one.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Stuck on 2D array issue
    By scottdaking in forum New To Java
    Replies: 2
    Last Post: 02-25-2013, 07:11 PM
  2. Another Array Issue (SOLVED)
    By Captain Forge in forum New To Java
    Replies: 1
    Last Post: 11-22-2012, 07:45 AM
  3. Array issue
    By daemonlies in forum New To Java
    Replies: 1
    Last Post: 04-30-2012, 12:23 AM
  4. Array game issue
    By Arom in forum New To Java
    Replies: 2
    Last Post: 11-30-2011, 08:33 PM
  5. Sorting Array Issue
    By larson1118 in forum New To Java
    Replies: 2
    Last Post: 04-21-2011, 05:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •