Is it possible to call 2D array in a method?
JButton[][] buttons = new JButton[3][3];
int number = 0;
for (int i = 0; i < buttons.length; i++) {
for (int j = 0; j < buttons.length; j++){
buttons[i][j] = new JButton(); // create buttons
frame.add(buttons[i][j]);
buttons[i][j].setText(".");
buttons[i][j].setName("button" + number);
number++;
}
}
I have tried this but it isn't working.
private void test(buttons[i][j]) {
}
edited: And also, would I need to use the same way to call all the buttons in other places, say for example I have this.
JButton[] button = {//call the array here of buttons here //};
Random rand = new Random();
JButton newbutton = button[rand.nextInt(button.length)];
I want to call all the buttons in the parameter of the button variable. the piece of code above will select one button at random by number of given buttons. for example, if i have;
JButton[] button = {button1, button2, button3, button4, button5, button6};
given the code above, it will select from one of the buttons in the list and then put the chosen button in the variable newbutton. but since I have used an array, I can't have button1, button2, button and so on in the random selection. So I was wondering if it is possible to somehow call the array inside this array of button. I have tried this but it doesn't work.
JButton[] button = {buttons[i][j]};