I’m trying to make a 2D grid of JTextFields to represent a level map. I’ve come to the following for initializing the 2D Array of TextFields:
fields = new TextField[level.rows][level.columns];
TextField field;
for (int r = 0; r < level.rows; r++) {
for (int c = 0; c < level.columns; c++) {
field = new TextField(level.bricks[r][c].type);
fields[r][c] = field;
}
Now I have to add them to the JFrame, but they need to be lined up so that every row gets under the other column. I did find GridLayout, however i’m not very experienced with AWT/Swing, and still don’t know how achieve the desired layout. I was expecting there to be some kind of method like gLayout.add(JComponent,row,column).
This should work as you explained, here’s a fully working example that puts
JLabelsin the 5×5 Grid:The end result would look something like this:
Hope this helps.