This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Cannot get a custom JPanel to resize when the outer panel is resized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Cannot get a custom JPanel to resize when the outer panel is resized" Watch "Cannot get a custom JPanel to resize when the outer panel is resized" New topic
Author

Cannot get a custom JPanel to resize when the outer panel is resized

Steve Stevenson
Greenhorn

Joined: Apr 12, 2012
Posts: 21
I am attempting to insert a JPanel into another JPanel so that when the outer JPanel resizes the inner JPanel will resize itself to match the size of the outer panel. I suppose I could listen for resize events, but I know there's a way to set this up automatically. In fact my outer panel already works this way!

My outer panel was positioned onto the JFrame using Netbean's design view, and whenever I resize the frame the outer panel automatically resizes. I considered stealing that code and reusing it for setting up my inner panel, but that auto-generated code is very complicated and uses GroupLayout. The docs don't seem to encourage its use because it states: "GroupLayout is intended for use by builders, but may be hand-coded as well."

I figured one of the general layout managers have to provide what I need, but I can't figure this out! The best I've been able to do is to get a panel that will adjust on the horizontal axis but the vertical axis always remains fixed.




My thinking for the sizes was that I didn't care how small or large the inner panel could be, and just put in a preferred size to get it to show up (I'll adjust these later), but what happens is that the horizontal size of 800 seems locked and cannot be changed.

I also tried BorderLayout and got the same results:




I do something similar in another part of my app and in that situation I use the mysterious GrigBagLayout. So I tried the same code here and the inner panel doesn't appear at all:




I should probably research that attempt a little more, but I've been banging my head into a wall for a couple hours now and getting nowhere. I don't know why these layout managers aren't clicking with me. I will read the doc, and things will make sense, but then when I try to apply it in the real world I almost always get stuck with questions that the docs neither answer or even address.



FYI:
The reason I'm attempting to do this is that my outer panel is a customer image & slideshow panel and my inner panel is something else that has graphics with alpha layers and other things on it. I don't want to merge the two into a single class that's subclassed from the outer.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this is dodgy, but it might give you something to play around with
(ComponentListener is probably the better way)

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4572
    
    5

Michael, without trying that code it looks to me that a call to pack() or, in a different hierarchy, revalidating the container of the outer panel, could lead to infinite recursion. This lineis equivalent toThat looks dicey to me.


luck, db
There are no new questions, but there may be new answers.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> That looks dicey to me.

totally agree - dodgy is the word I used :-)
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4572
    
    5

Rob Camick's Relative Layout, with weights of [1,2,1], nested vertical and horiizontal, should be able to do the trick.
Steve Stevenson
Greenhorn

Joined: Apr 12, 2012
Posts: 21
I'm actually surprised that there isn't a simple solution that involves having a layout manager handle this automatically. Since there wasn't, and Michael Dunn's example was both dodgy and dicey (Thanks for the demo anyway it was useful to test out and it did work without any problems) I have decided just to listen for resize notifications on the outer panel. Another surprising thing is that there doesn't seem to be a resize notification for containers; it only seems like the notification is available for components. Fortunately that wasn't an issue today, but someday that seems like it will be an issue.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4572
    
    5

Steve Stevenson wrote:Another surprising thing is that there doesn't seem to be a resize notification for containers; it only seems like the notification is available for components.

java.awt.Container extends java.awt.Component. Every Container is-a Component.
Steve Stevenson
Greenhorn

Joined: Apr 12, 2012
Posts: 21
Darryl Burke wrote:
Steve Stevenson wrote:Another surprising thing is that there doesn't seem to be a resize notification for containers; it only seems like the notification is available for components.

java.awt.Container extends java.awt.Component. Every Container is-a Component.


Hum... I didn't think of that. It is somewhat strange that I found comments online of people saying it wasn't working properly for objects known as being containers, but then again it's possible other things could have been preventing this.


I'm still loving the fact that this issue introduced me to invokeLater. I had seen it before but never looked into it, and all this time I had been wishing for something along the lines of PostMessage from the Windows API. Now I'm planning to overuse and abuse that function a little bit
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4572
    
    5

Maybe you'd like to read up on Concurrency in Swing.

Did you try Rob Camick's Relative Layout? It does look like that would give you a better, cleaner solution to your need.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 499

but that auto-generated code is very complicated...

Why not you use JScrollPane for instead of outer big jpanel... may be my suggestion will be wrong for your question...but its just suggestion...
Every Container is-a Component...


Millions saw the apple fall, but Newton asked why.
 
GeeCON Prague 2014
 
subject: Cannot get a custom JPanel to resize when the outer panel is resized