Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

GridLayout not working?  RSS feed

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ladies/gents,

I'm having a problem with the GridLayout. I've set it to (2, 3), and from what I understand, components are added from top to bottom, left to right. Why, then, is my np (NumberPanel) being pushed down to the next row, instead of populating in column number 3 on the top row? Any ideas? Also, setSize() does nothing to modify the size of this JFrame???

Thanks in advance!
Main class;

ColorPanel class:


NumberPanel class:

ShapePanel class:
 
Harold Lime
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/GridLayout.html

Says they are added left to right, then top to bottom.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harold Lime wrote:http://java.sun.com/j2se/1.4.2/docs/api/java/awt/GridLayout.html

Says they are added left to right, then top to bottom.


Yeah, so, I should have my top row of 3 columns populated first, right? Then my second row?
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the number of rows is larger than 0, the number of columns specified is ignored and instead there will be "(ncomponents + nrows - 1) / nrows" columns, where ncomponents is the number of components and nrows is the number of rows. In your code, ncomponents is 3 and nrows is 2, so the number of columns will be 2.

This is also mentioned in the API:
When both the number of rows and the number of columns have been set to non-zero values, either by a constructor or by the setRows and setColumns methods, the number of columns specified is ignored. Instead, the number of columns is determined from the specified number of rows and the total number of components in the layout.

So if you want to use a fixed number of columns, just set the number of rows to 0. It will then respect the column count and just keep filling until all components are put in place.
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:If the number of rows is larger than 0, the number of columns specified is ignored and instead there will be "(ncomponents + nrows - 1) / nrows" columns, where ncomponents is the number of components and nrows is the number of rows. In your code, ncomponents is 3 and nrows is 2, so the number of columns will be 2.

This is also mentioned in the API:
When both the number of rows and the number of columns have been set to non-zero values, either by a constructor or by the setRows and setColumns methods, the number of columns specified is ignored. Instead, the number of columns is determined from the specified number of rows and the total number of components in the layout.

So if you want to use a fixed number of columns, just set the number of rows to 0. It will then respect the column count and just keep filling until all components are put in place.


Ah, hah! Thanks, Rob!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!