Skip to content

Instantly share code, notes, and snippets.

@brycethomas
Created August 18, 2011 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brycethomas/1153191 to your computer and use it in GitHub Desktop.
Save brycethomas/1153191 to your computer and use it in GitHub Desktop.
A 2-dimensional array of JButtons and setting text on them
// an array of JButtons
JButton[][] buttons = new JButton[4][4];
/* actually creates each of the sixteen JButton objects and sets
each button's text to look something like "Button [1][2]" (depending on row and column) */
for (int i=0; i < buttons.length; i++) {
for (int j=0; j < buttons[i].length; j++) {
buttons[i][j] = new JButton("Button [" + i + "][" + j + "]");
}
}
// set the text later on some specific button if we want
buttons[2][3].setText("changed text");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment