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.
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 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.
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, 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.
There are no new questions, but there may be new answers.
Rob Camick's Relative Layout, with weights of [1,2,1], nested vertical and horiizontal, should be able to do the trick.
Joined: Apr 12, 2012
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.
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