As part of the chapter on layout managers, I decided it would be a good idea to have an example of a custom layout manager. Personally, I've always found that the need for a custom layout manager is driven more by form than function. You can almost always nest enough panels to get the layout you need with the managers included in the JDK. However, what results is not always the most elegant solution.
Lets take this layout as an example:
Basically you have a layout that is defined by the order that components are added to the container. The main characteristic is that as each component is added it gets all the space on its edge unlike BorderLayout. Multiple components can be added to each edge as well. Accomplishing this with only BorderLayout results in:
Now let me introduct EdgeLayout. I'll mention that I haven't searched to see if a layout manager implementing similar functionality already exists. EdgeLayout is writt
1f40
en specifically to the requirements above. Using it for our example results in:
Our example went from 38 lines to 14. Not bad if I do say so myself. This is a first draft on the code. Suggestions, comments, and questions are always welcome. While this layout manager is more of a proof of concept, I see no reason why it couldn't be used to assist day to day development.