Back to the index

Getting components to grow and fill using the JGoodies FormLayout

Summary

I've been really pleased with the FormLayout from JGoodies. In this article I'd like to show how to get components to properly resize, both horizontally and vertically, when a user resizes a JDialog that contains these components. I'll demonstrate this for both JTextField's as well as a JTable/JScrollPane component.

Introduction

Let me begin with one caution: This tutorial is for relative newbies to the FormLayout, but ... you will need to have some experience with this layout, as I'm going to jump right through some of the code without much explanation. If you're absolutely new to the FormLayout I suggest visiting the tutorial at this link.

Beginning with the end in mind, let me first show the dialog that I'm creating. Figure 1 shows my sample "Edit Project" dialog.

Figure 1: This view of the "Edit Project" dialog shows the dialog that I'm about to create.


Figure 1 shows what this JDialog looks like when it is first instantiated. However, because I'm letting the user resize the dialog, if I create my FormLayout improperly I can easily create a dialog as shown in Figure 2, where the fields in the dialog do not properly resize horizontally or vertically. To be clear, this is a poor implementation of this dialog.

Figure 2: This figure shows that the "Edit Project" dialog should *not* look like when it is resized.

Conversely, Figure 3 shows what the dialog should look like when it is resized. All of the data entry fields resize as the dialog is expanded horizontally, and the Configuration field (a JTable in a JScrollPane) expands vertically when the user makes the dialog taller. This effect is easy to create with the JGoodies FormLayout; you just need to add a few key labels when creating the layout.

Figure 3: This figure shows that the "Edit Project" dialog should look like when it is resized.


 
Next: A brief look at the wrong approach>>




copyright 2008, devdaily.com, all rights reserved.
devdaily.com, an alvin alexander production.