File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Simple GUI program:  changing color of a panel. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Simple GUI program:  changing color of a panel." Watch "Simple GUI program:  changing color of a panel." New topic
Author

Simple GUI program: changing color of a panel.

John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
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.



www.cs-repository.info
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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

Joined: May 23, 2008
Posts: 215
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple GUI program: changing color of a panel.
 
Similar Threads
ActionListener not recognized
TwoButtons App From "HeadFirst Java" Book
random colour problems
Simple Gui Question (Head First Java)
Issues with the setting up of Actionlisteners for JButtons