Saturday, February 17, 2007

Defining a baseline alignment for your custom component on Matisse

Some days ago, I needed refine some custom componets on my project, and since the component super class is a JPanel, the NetBeans form editor (aka Matisse), uses just the bottom or top border to align components on the form. Any way, there is a way to informe for Matisse the baseline value, which will be useful for a good GUI design. My example is a simple search panel, where there is a text field where user define a ID that will be searched, and some description will be showed for user at another one. So, to do this component I use just two JTextFields and a JPanel. The image below show the three alignment possibilities on Matisse, top, bottom and baseline:

Example for top, bottom and baseline alignment. (Click to enlarge)

So, the baseline alignment just is possible if I add the follow method on my component:

public int getBaseline( int w, int h ) {

return h * 3 / 4;


It is not necessary implements any interface or another artefact, just add this method, which return a distance for baseline alignment since top border. The example above is just a simple example. This behavior are define on org.jdesktop.layout.Baseline class (see javadoc), and if the custom component is a subclass of a known component, as JButton and so on, this is not necessary.

No comments: