• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GridLayout not working?

 
Gunslinger
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic