Results 1 to 7 of 7
Thread: How to loop through 2D array?
- 12-04-2012, 12:59 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 30
- Rep Power
- 0
How to loop through 2D array?
I have got a 2D array of JButtons:
I am wondering how I would loop through this array to find something say in [3,3] ?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 } }
When I click on a JButton I convert the row/col to ints:
This stores them and I want to search for them later...
This is what I have so far..Java Code:int button1column = (Integer) btn.getClientProperty("column"); //Parse column value into int int button1row = (Integer) btn.getClientProperty("row"); //Parse row value into int
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 itJava 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"); } } }
Thank you
- 12-04-2012, 01:16 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 12-04-2012, 01:21 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 30
- Rep Power
- 0
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?
- 12-04-2012, 01:31 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 12-04-2012, 01:34 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 30
- Rep Power
- 0
Re: How to loop through 2D array?
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: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 get the error "array required. but java.swing.JButton found" :/Java Code:JButton leftbtn = button[button1rowLeft][button1column];
- 12-04-2012, 02:30 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 12-04-2012, 02:39 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 30
- Rep Power
- 0
Similar Threads
-
[Problem] Enhanced for-loop with 2D arrays (or array in array)
By thewrongsyntax in forum New To JavaReplies: 0Last Post: 10-07-2012, 08:49 PM -
enhanced 4 loop 4 2d array (mod. edit: enhanced for-loop for 2D array)
By java4amanda in forum New To JavaReplies: 5Last Post: 03-29-2012, 06:22 PM -
For loop array
By TheCase in forum New To JavaReplies: 15Last Post: 12-23-2011, 05:34 PM -
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 02:12 AM -
Loop through Array in JSP
By Robert_85 in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 04-25-2010, 09:00 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks