Results 1 to 7 of 7
  1. #1
    sim18 is offline Member
    Join Date
    Oct 2011
    Posts
    30
    Rep Power
    0

    Default How to loop through 2D array?

    I have got a 2D array of JButtons:

    Java Code:
    JButton[][] button = new JButton[8][8]; //Sets new JButtons
            this.setLayout(new GridLayout(7, 7)); //Grid layout 7,7
    
    for(int i = 0; i < 7; i++) {
                for(int j = 0; j < 7; j++) {
                    button[i][j] = new JButton(peg); //Populates grid with ImageIcons
                    button[i][j].putClientProperty("column", i); //Stores all column properties
                    button[i][j].putClientProperty("row", j); //Stores all row properties
                    add(button[i][j]); //Adds all buttons into gridLayout
                }
            }
    I am wondering how I would loop through this array to find something say in [3,3] ?

    When I click on a JButton I convert the row/col to ints:
    This stores them and I want to search for them later...

    Java Code:
    int button1column = (Integer) btn.getClientProperty("column"); //Parse column value into int
    int button1row = (Integer) btn.getClientProperty("row"); //Parse row value into int
    This is what I have so far..

    Java Code:
    public void checkButton(int button1row, int button1column)
        {
            for(JButton button ; button < ; button++) {
                    if(j == button1column && i = button1rowLeft)
                    {
                        System.out.println("Found the button you clicked on");
                    }
            }
        }
    I know this doesn't work but I want to take the row/col values that I just clicked on and loop through the array until I find it

    Thank you

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: How to loop through 2D array?

    If you have the array then why not simply go:
    Java Code:
    JButton clickedButton = button[button1row][button1column]; // or the other way around...
    ?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    sim18 is offline Member
    Join Date
    Oct 2011
    Posts
    30
    Rep Power
    0

    Default Re: How to loop through 2D array?

    Thanks very much for your reply :)

    I get how that works, but what if I wanted to click on a JButton, and then get the properties to the button left of that without actually clicking on it?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: How to loop through 2D array?

    Well, "left" would be -1 from the column index.
    "right" would be +1.
    "up" would be -1 from the row index.
    "down" would be +1.

    Don't forget top check the selected button is not already on the far left/right/top/bottom.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    sim18 is offline Member
    Join Date
    Oct 2011
    Posts
    30
    Rep Power
    0

    Default Re: How to loop through 2D array?

    Java Code:
    for(int i = 0; i < 7; i++) {
                for(int j = 0; j < 7; j++) {
                    button[i][j] = new JButton(peg); //Populates grid with ImageIcons
                    button[i][j].putClientProperty("column", i); //Stores all column properties
                    button[i][j].putClientProperty("above", i-1);
                    button[i][j].putClientProperty("below", i+1);
                    button[i][j].putClientProperty("row", j); //Stores all row properties
                    button[i][j].putClientProperty("left", j-1);
                    button[i][j].putClientProperty("right", j+1);               
                    button[i][j].addActionListener(new MyActionListener());
          
                    add(button[i][j]); //Adds all buttons into gridLayout
                }
            }
    I have added that but later in my code when I click on a button and want to find out what is left of that button:

    Java Code:
    JButton leftbtn = button[button1rowLeft][button1column];
    I get the error "array required. but java.swing.JButton found" :/

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: How to loop through 2D array?

    Well, is 'button' an array?
    I see from your earlier code you have a JButton called 'button'.
    So are you possibly mixing up your variable names?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    sim18 is offline Member
    Join Date
    Oct 2011
    Posts
    30
    Rep Power
    0

    Default Re: How to loop through 2D array?

    Yes, the code works now I did : public static JButton[][] button;

Similar Threads

  1. Replies: 3
    Last Post: 12-28-2013, 09:39 PM
  2. Replies: 5
    Last Post: 03-29-2012, 06:22 PM
  3. For loop array
    By TheCase in forum New To Java
    Replies: 15
    Last Post: 12-23-2011, 05:34 PM
  4. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  5. Loop through Array in JSP
    By Robert_85 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-25-2010, 09:00 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
  •