Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple GUI program: changing color of a panel.

 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Need your help to sort out the below problem.

This is a beginner GUI program - Changing the color of a panel when a button is clicked. This button is in the top panel and the color panel is the bottom one.



This program might look bit lengthly and messed up. My apologies!

This program is compiled within Netbeans but does't show the bottom panel when run.


 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since it does not have any child components, it's preferred size is 0x0. BorderLayout does maintain the preferred height for the NORTH and SOUTH components, and the preferred width for the WEST and EAST components. The CENTER component gets what's left. So your frame has a panel the size of the button at the top, a panel with height 0 at the bottom, and nothing in the center.

Either give the panel a preferred size, or add it using BorderLayout.CENTER.
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! that was a quick reply! Thanks Rob.

I hadn't thought about BorderLayout.CENTER but i had tried panel.setSize(200, 200) with BorderLayout.SOUTH and it didn't work!
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because almost all layout managers completely ignore any size you give it using setSize. Only the minimum, preferred and maximum sizes are sometimes respected.
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic