Results 1 to 13 of 13
  1. #1
    Chas3down is offline Member
    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Default Array of JButtons

    Java Code:
    public class tuna extends JFrame {
    
        public tuna(){
            super("the title");
            setLayout(new GridLayout(3,3));
            JButton btnBoard[][] = new JButton[2][2];
    
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    btnBoard[i][j] = new JButton("");
                    btnBoard[i][j].setPreferredSize(new Dimension(50,50));
                }
            }
    
            add(btnBoard[3][3]);
    
        }
    }
    I get
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at the line add(btnBoard[2][2]);

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Array of JButtons

    btnBoard is an array with a size of 2, therefore valid indicies are 0, 1. The nested loops continue while i and j < 3, therefore valid values are 0, 1, 2. So what do you think will happen when i or j or both are 2?

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: Array of JButtons

    Any particular reason you need to save the references in an array?

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

  4. #4
    Chas3down is offline Member
    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Default Re: Array of JButtons

    I tried 3,3 and I still got out of bounds exc.


    Jim, I am trying to do this for learning purposes.
    Java Code:
    public tuna(){
            super("the title");
            setLayout(new GridLayout(3,3));
            JButton btnBoard[][] = new JButton[3][3];
    
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    btnBoard[i][j] = new JButton("");
                    btnBoard[i][j].setPreferredSize(new Dimension(50,50));
                }
            }
    
            add(btnBoard[3][3]);
    
        }
    
    and 
    
    public tuna(){
            super("the title");
            setLayout(new GridLayout(2,2));
            JButton btnBoard[][] = new JButton[3][3];
    
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    btnBoard[i][j] = new JButton("");
                    btnBoard[i][j].setPreferredSize(new Dimension(50,50));
                }
            }
    
            add(btnBoard[2][2]);
    
        }
    both will give an error
    Last edited by Chas3down; 09-19-2013 at 04:02 AM.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Array of JButtons

    :headdesk:

    Of course 3,3 is out of bounds as well. Once again the array is 2 by 2 so valid indicies are 0 and 1. which means that 2 is invalid, 3 is invalid, 4 is invalid etc.

  6. #6
    Chas3down is offline Member
    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Default Re: Array of JButtons

    ugh, my bad i keep on posting in the code wrong.
    The array is 3x3... (0,0) -> (2,2)

    Java Code:
    public tuna(){
            super("the title");
            setLayout(new GridLayout(2,2));
            JButton btnBoard[][] = new JButton[2][2];
    
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    btnBoard[i][j] = new JButton("");
                    btnBoard[i][j].setPreferredSize(new Dimension(50,50));
                }
            }
    
            add(btnBoard[2][2]);
    
        }
    btnBoard[2][2] is a 3x3 array

    and

    for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
    btnBoard[i][j] = new JButton("");
    btnBoard[i][j].setPreferredSize(new Dimension(50,50));
    }
    }

    will create 9 buttons. This should work.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Array of JButtons

    Quote Originally Posted by Chas3down View Post
    btnBoard[2][2] is a 3x3 array
    Absolutely wrong.

    ONCE AGAIN, for a 2 by 2 array the valid indicies are 0 and 1.

  8. #8
    Chas3down is offline Member
    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Default Re: Array of JButtons

    Yes i know.......


    btnboard[2][2]
    0,1,2 and 0,1,2.. which is 3x3

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Array of JButtons

    NO NO NO NO NO NO

    When you declare an array the number inside the bracket is the size and not the highest index.
    Java Code:
    int[] arr = new int[5];
    That creates an array that has 5 buckets and those buckets are labeled 0 1 2 3 & 4. Remember that the highest index is always one less than the size. If you want an array with a size of 3 by 3 then you need to use 3 inside the brackets.

  10. #10
    Chas3down is offline Member
    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Default Re: Array of JButtons

    Oh.... thank you, I understand!!
    I also have to add each button add(btnBoard[i][j]); in the for loop, didn't like me trying to add the 2d array.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: Array of JButtons

    I'll ask this again. Why are you saving the references of the buttons in an array? In most cases, they aren't really needed.

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

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Array of JButtons

    Does the add method that accepts a 2D JButton array as a parameter exist?

  13. #13
    Chas3down is offline Member
    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Default Re: Array of JButtons

    Jim - it is not needed, I am making a tictactoe game, I know I could do this easily without a 2D array, I just want to make a 2D array for learning purposes.

    Junky - yes, but I get a run time error.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at tuna.<init>(tuna.java:25)

    which is, add(btnBoard[3][3]);



    so instead of doing that, I have to do "add(btnBoard[i][j]);" in the for loop.

    But now that I am thinking ahead, I made need to add the buttons to a panel and then add a panel because I am having some sizing issues.

Similar Threads

  1. How can I make an array of JButtons?
    By LuxoJr in forum New To Java
    Replies: 2
    Last Post: 02-27-2012, 03:51 AM
  2. Replies: 8
    Last Post: 04-22-2011, 01:55 PM
  3. Creating array of JButtons
    By tabchas in forum New To Java
    Replies: 20
    Last Post: 04-16-2011, 05:36 AM
  4. jbuttons
    By sarah jain in forum AWT / Swing
    Replies: 3
    Last Post: 03-16-2011, 07:13 AM
  5. 2D Array of JButtons
    By stevemcc in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2008, 11:42 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
  •