• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

GridLayout not working?

 
Gunslinger
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:
 
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
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?
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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
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!
 
I will suppress my every urge. But not this shameless plug:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic