Thursday 14 May 2015

Custom Layouts in Java


You can develop your own Layout Managers by creating a new class that implements LayoutManager and overrides the following five methods.
addLayoutComponent(String area_name, Component a_component)
Adds a component to the layout. If your layout has multiple named areas (such as North in BorderLayout), the name specifies which region the component should go to. If your layout doesn't keep any special information about the component, you can make this method do nothing.
removeLayoutComponent(Component c)
Removes a component from the layout. If you don't keep any special information about a component, you can make this method do nothing.
Dimension, preferredLayoutSize(Container the_parent)
Computes the preferred size for the container that holds the layout.
Dimension, minimumLayoutSize(Container the_parent)
Returns the minimum size needed for the layout.
layoutContainer(Container the_parent)
Lays out the components using the reshape method. This is where you put the logic for deciding how to position components.
Although implementing a custom Layout Manager isn't earth-shatteringly complex, the good news is that there are lots of people in the Java community-and some already are making custom Layout Managers. You can get a PackerLayout, for example, which is very similar to the layout approach used in tcl/tk-another mainly UNIX programming language. Before spending the time building your own Layout Manager, do a Web search to make sure that you can't save the time by using someone else's work.

Here are some sample Layout Managers that you can get on the Web. I haven't tried them, so don't view this as a recommendation; instead, it merely should be a starting point in your quest to avoid writing any more code than necessary.  

Note
One problem with custom Layout Mangers is that they have to be downloaded over the network, while the AWT Layout Managers already reside on the local machine. Because a Layout Manager can be large (more than 10 KB), this is an issue if a lot of your users will be working with 14.4-Kbps or 28.8-Kbps modems. If your users have T1 lines, it's not an issue.



No comments:

Post a Comment