• Post Reply Bookmark Topic Watch Topic
  • New Topic

GridBagLayout with glue, w/o gaps

 
Peter Merker
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm still working on the Tower of Hanoi Game with which I already bugged Craig
so much (thanks again, Craig!). I took some time off because I became too
frustrated with the troubles of realizing a layered design. So now I chose to
start again with a GridBagLayout. Whilst resizing the three rectangles works
well, the vertical positioning still doesn't work. They should stay on top of
each other at the bottom of the panel, without any gaps in between. There
should only be a free space at the right and the top if the frame becomes
bigger than added component sizes. I thought that specifying anchor set
to PAGE_END and fill set to BOTH combined with a glue component would do
this. Somehow not. Any ideas? Here's the whole code:


--

--
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you perhaps review what you are trying to do, or give us a link to the other thread?

Usually when using a GridBagLayout, glue isn't needed...
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About the resizing from within paint code. I would try to avoid this. I understand the need for getting size information at runtime. I don't see enough of your overall design strategy to suggest an alternative yet.

Here's a suggestion for the control of spacing between discs. The code is set up for center alignment with an option for left alignment.
 
Peter Merker
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again for your corrections, Craig!
The paperweight is a good idea, but it still shows a strange behaviour if resized. It's possible that you have gaps while resizing and they can also stay afterwards. I guess I will now try another solution similar to the paperweight idea. A friend of mine pointed me in the direction of using invisible discs, so that you have a constant total of discs on the tower. Even if this works, it seems to me not to be really "professional", rather like a trick. I can't imagine that there aren't Swing methods/concepts to solve such a task.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There may be a lot of ways to solve this. I solved it in two ways. Both ways use the Disc class and treat the discs as components that paint themselves in their host container. I create the discs in their final sizes and add/remove them to the tower components as needed. I think your resizing code, computed in paintComponent and passed into Disc with setRectangleSize is the origin of your resizing difficulties.

The first and easier way is with null layout. In this I only needed to keep track of the height of the last component added, the discs are centered and set at the current height datum (member variable). I wrote this one last week and then lost it when I cleared out my files Sunday. Then rewrote it yesterday after seeing your post.

The second way is with GridBagLayout which is a little more involved. I used the paperweight trick in it. Without the paperweight it would require setting the weighty constraint with every add/remove operation. I also used another JLabel component at the bottom of the tower JPanel, dynamically resizable, to act as a spacer for the discs above. Some of the challenges in this approach included getting the code fine–tuned to remove and replace the components below the paperweight for add/remove ops with the discs, and wiring up the ComponentListener to avoid initialization problems. Last week I had given up on trying this with GridBagLayout. Yesterday it seemed to flow together.

I would say that java is well–equipped with options to solve this challenge. The question is how do you want to go about it. I enjoy your zeal, desire to learn and strong determination. It's been fun.
[ September 16, 2004: Message edited by: Craig Wood ]
 
Peter Merker
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig, as I wrote "Even if this works, it seems to me not to be really "professional", rather like a trick. I can't imagine that there aren't Swing methods/concepts to solve such a task." I should have written "Even if this will work..." as I was referencing to the proposal of my friend. I just noticed now that it sounded a bit offensive. As you might have noticed, english is not my native tongue. So sometimes misunderstandings occur.
It just seems strange to me to use "empty" components as a filler. I think there should be something like a leaveEmptySpace method and thought glue would work so, but it doesn't.
May I have a look at your working code with GridBagLayout?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't take offense at what you said; it seemed straight–forward in a good way. To use this you first click on the tower you want the disc to move to (target) and then click on the tower that contains the disc that you want to transfer (to the target). I want to change it to the opposite as it seems to be more intuitive to select the disc first and the target second.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!