I was doing Chapter No. 13, from O' Reilly's Head First Java, and was making up the "BeatBox App" as dubbed by the book. The program compiles fine, but the checkboxes are not visible. here is the code -
The output is -
I think I've added the checkboxes correctly, but I'm not so sure about the grid layout.
You are creating 256 checkboxes, all lined up side by side in a single row. There is not enough room on your screen to draw them - even if your screen is 1760 pixels wide, with your layout each button gets only about 4 pixels, so with the border there is no room to draw anything. Try it with 4 checkboxes and see what happens. then with 20, or 50
Why do you use
Make up your mind: the pack() call discards the result of the setBounds() and sets the size to the preferred size of all of the contents.
And why use a GridLayout without specifying the number of rows and columns?
Fred Kleinschmidt wrote:You are creating 256 checkboxes, all lined up side by side in a single row. There is not enough room on your screen to draw them - even if your screen is 1760 pixels wide, with your layout each button gets only about 4 pixels, so with the border there is no room to draw anything. Try it with 4 checkboxes and see what happens. then with 20, or 50
Why do you use
Make up your mind: the pack() call discards the result of the setBounds() and sets the size to the preferred size of all of the contents.
And why use a GridLayout without specifying the number of rows and columns?
Thank you fir the quick reply. GridLayout isn't actually taught in the book, so I copied it thinking it must be correct. I'll set the no. of columns and rows now, searching for it