How to create a custom layout manager.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the section on controlling the appearance of your form in the Advanced Form Topics chapter in the Programmer's Guide |
AgfForm has an addControls()
method that calls addAllFormControls()
, a method that SilverStream generates based on the controls and relative postions that were added in the Form Designer.
In this example the controls were deliberately misaligned in the Designer. When you run the form, addControls()
is called and the controls appear in the absolute positions that were set at design time. When you select the button on the form, a custom method creates a new layout with the controls realigned neatly on the display.
Selecting the button labeled "Improve the Layout" invokes a user-defined method called setControls()
. This method specifies a new layout manager that dynamically adds the controls and realigns them on the display. Here is the code and comments for this method
protected void setControls() { // Specify BorderLayout for the form. setLayout(new BorderLayout()); // Create a container panel JPanel jpContainer = new JPanel(new BorderLayout()); jpContainer.setOpaque(false); // Add the title to the north region of the form's // border layout. jpContainer.add(lblTitle, BorderLayout.NORTH); // Add 3 buttons to a FlowLayout panel so they will be // equally sized and centered. JPanel jpNavigation = new JPanel(new FlowLayout()); jpNavigation.setOpaque(false); jpNavigation.add(btnFirst); jpNavigation.add(btnPrev); jpNavigation.add(btnNext); jpNavigation.add(btnLast); // Add the panel to the South region of the form's // border layout. jpContainer.add(jpNavigation, BorderLayout.SOUTH); // Add 4 labels and text fields to the center of the // form's border layout. Each label and text field is in // a Box container, arranged vertically Box boxEmpID = new Box(BoxLayout.Y_AXIS); boxEmpID.add(lblEmpID); boxEmpID.add(fldEmpID); Box boxName = new Box(BoxLayout.Y_AXIS); boxName.add(lblName); boxName.add(fldName); Box boxBirthdate = new Box(BoxLayout.Y_AXIS); boxBirthdate.add(lblBirthdate); boxBirthdate.add(fldBirthdate); Box boxHiredate = new Box(BoxLayout.Y_AXIS); boxHiredate.add(lblHiredate); boxHiredate.add(fldHiredate); // The four boxes are added to a containing box. // They are arranged horizontally in the container. Box boxFields = new Box(BoxLayout.X_AXIS); boxFields.add(boxEmpID); boxFields.add(boxName); boxFields.add(boxBirthdate); boxFields.add(boxHiredate); // The containing box is added to the north region of a // JPanel so the fields can't be resized vertically. JPanel jpFields = new JPanel(new BorderLayout()); jpFields.setOpaque(false); jpFields.add(boxFields, BorderLayout.NORTH); // The panel is added to the center region of the // form's border layout. jpContainer.add(jpFields, BorderLayout.CENTER); this.add(jpContainer,BorderLayout.NORTH); // Any controls that need to respond to events get listeners. btnLast.addActionListener(this); btnNext.addActionListener(this); btnPrev.addActionListener(this); }