Possible Duplicate:
Swing layman pagination
I have a paged document represented in my program (structure of JPanels), and I have to keep track of all the elements that are being added and removed to the document, as well as their size and move them inbetween pages accordingly.
I have come up with a way of managing this, writing an algorithm that checks the height of the page and free space remaining and acting upon that. This is flawed because if the height of a component is reduced enought, or it's really high and then removed, more than one components from the next page can be moved. Also, if you reduce the height of the first component, with it's new height, it may fit in the free space of the previous page. Combine the two last sentences, and you have a hell to code, esspecially since all the code is within a listener.
So, what if, instead of having a bunch of pages, I have a single Component.
That Component would have blank spaces with fixed height (representing the page) and "solid" spaces (representing the footer of top page, inter-paginal space and header of the bottom page).
And then, when elements are added to the Component, they would only be added to the blank spaces, skipping the solid ones.
The catch would be to be able to tell which component is on which blank space, and not having to worry about moving elements inbetween the blank spaces due to element removal, addition or resizeing. That means when the first blank space is all filled up, the next element is automatically added to the next blank space. This also means that upon removal of the element, Component would automatically check if it can fit an additinal component to that blank space and then pull it from the next one.
Anyway, although what I have written above may not be possible, I hope you get the idea of what I'm trying to achieve here.
If you have any ideas that would make writing that part of code easier, please tell me what could I do.