 |
Home | Changes | Index | Search | Go
First of all I am not a specialist in layout managers but I have used the layout managers included in the AWT and Swing.
And... after a while I decided that it's quite difficult sometimes to get what you want using them.
So, searching for some third party layout manager I have found FormLayout, http://jgoodies.com/downloads/libraries.html.
It's quite intuitive to use it and after you use it for a while you become quite productive with it.
I haven't tried other layout managers because I am satisfied with this one. But maybe I should try others ....
Here is the source code:
import javax.swing.*;
import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;
/**
* <p>Test for {@code FormLayout}.</p>
*
* @version 1.0 Oct 13, 2006
* @author Sarmis Mihail Bulau
*/
public class AddressBookPanel extends JPanel {
/**
* <p>Creates a default {@code AddressBookPanel} instance.</p>
*/
public AddressBookPanel() {
createComponents();
initComponents();
}
private void initComponents() {
setLayout( new FormLayout(
new ColumnSpec[] {
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec( ColumnSpec.FILL, Sizes.DEFAULT, 0.3 ),
FormFactory.UNRELATED_GAP_COLSPEC,
new ColumnSpec( ColumnSpec.RIGHT, Sizes.DEFAULT, FormSpec.NO_GROW ),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec( ColumnSpec.FILL, Sizes.PREFERRED, 0.3 ),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
FormFactory.PREF_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec( ColumnSpec.FILL, Sizes.DEFAULT, 0.3 ),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
},
new RowSpec[] {
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
new RowSpec( RowSpec.TOP, Sizes.DEFAULT, FormSpec.DEFAULT_GROW ),
FormFactory.LINE_GAP_ROWSPEC,
} )
);
initList();
add( new JScrollPane( list ), "2, 2, 1, 17" );
lastNameLabel.setText( "Last Name" );
add( lastNameLabel, "4, 2" );
add( lastNameField, "6, 2" );
initFirstNamePanel();
add( firstNamePanel, "8, 2, 5, 1" );
phoneLabel.setText( "Phone" );
add( phoneLabel, "4, 4" );
add( phoneField, "6, 4" );
initEmailPanel();
add( emailPanel, "8, 4, 5, 1" );
firstAddressLabel.setText( "Address 1" );
add( firstAddressLabel, "4, 6" );
add( firstAddressField, "6, 6, 7, 1" );
secondAddressLabel.setText( "Address 2" );
add( secondAddressLabel, "4, 8" );
add( secondAddressField, "6, 8, 7, 1" );
cityLabel.setText( "City" );
add( cityLabel, "4, 10" );
add( cityField, "6, 10" );
stateLabel.setText( "State" );
add( stateLabel, "4, 12" );
add( stateField, "6, 12" );
initPostalCodePanel();
add( postalCodePanel, "8, 12, 5, 1" );
countryLabel.setText( "Country" );
add( countryLabel, "4, 14" );
add( countryField, "6, 14" );
initButtonsPanel();
add( buttonsPanel, "4, 18, 9, 1" );
}
private void createComponents() {
list = new JList();
lastNameLabel = new JLabel();
lastNameField = new JTextField();
firstNamePanel = new JPanel();
firstNameLabel = new JLabel();
firstNameField = new JTextField();
phoneLabel = new JLabel();
phoneField = new JTextField();
emailPanel = new JPanel();
emailLabel = new JLabel();
emailField = new JTextField();
firstAddressLabel = new JLabel();
firstAddressField = new JTextField();
secondAddressLabel = new JLabel();
secondAddressField = new JTextField();
cityLabel = new JLabel();
cityField = new JTextField();
stateLabel = new JLabel();
stateField = new JTextField();
postalCodePanel = new JPanel();
postalCodeLabel = new JLabel();
postalCodeField = new JTextField();
countryLabel = new JLabel();
countryField = new JTextField();
buttonsPanel = new JPanel();
newButton = new JButton();
deleteButton = new JButton();
editButton = new JButton();
saveButton = new JButton();
cancelButton = new JButton();
}
private void initList() {
list.setModel( new AbstractListModel() {
String[] values = { "Bunny, Bugs", "Cat, Sylvester", "Coyote, Wile",
"Devil, Tasmanian", "Duck, Daffy", "Fudd, Elmer", "Le Pew, Pepe",
"Martian, Marvin" };
public int getSize() {
return values.length;
}
public Object getElementAt( int i ) {
return values[ i ];
}
} );
}
private void initFirstNamePanel() {
firstNamePanel.setLayout( new FormLayout(
new ColumnSpec[] {
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec( "fill:default:grow" ),
},
RowSpec.decodeSpecs( "default" ) )
);
firstNameLabel.setText( "First Name" );
firstNamePanel.add( firstNameLabel, "1, 1" );
firstNamePanel.add( firstNameField, "3, 1" );
}
private void initEmailPanel() {
emailPanel.setLayout( new FormLayout(
new ColumnSpec[] {
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec( "fill:default:grow" ),
},
RowSpec.decodeSpecs( "default" ) )
);
emailLabel.setText( "Email" );
emailPanel.add( emailLabel, "1, 1" );
emailPanel.add( emailField, "3, 1" );
}
private void initPostalCodePanel() {
postalCodePanel.setLayout( new FormLayout(
new ColumnSpec[] {
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec( "fill:default:grow" ),
},
RowSpec.decodeSpecs( "default" ) )
);
postalCodeLabel.setText( "Postal Code" );
postalCodePanel.add( postalCodeLabel, "1, 1" );
postalCodePanel.add( postalCodeField, "3, 1" );
}
private void initButtonsPanel() {
buttonsPanel.setLayout( new FormLayout(
new ColumnSpec[] {
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,
},
RowSpec.decodeSpecs( "default" ) )
);
( ( FormLayout )buttonsPanel.getLayout() ).setColumnGroups(
new int[][] { { 1, 3, 5, 7, 9 } } );
newButton.setText( "New" );
buttonsPanel.add( newButton, "1, 1" );
deleteButton.setText( "Delete" );
buttonsPanel.add( deleteButton, "3, 1" );
editButton.setText( "Edit" );
buttonsPanel.add( editButton, "5, 1" );
saveButton.setText( "Save" );
buttonsPanel.add( saveButton, "7, 1" );
cancelButton.setText( "Cancel" );
buttonsPanel.add( cancelButton, "9, 1" );
}
private JList list;
private JLabel lastNameLabel;
private JLabel firstNameLabel;
private JLabel phoneLabel;
private JLabel emailLabel;
private JLabel firstAddressLabel;
private JLabel secondAddressLabel;
private JLabel stateLabel;
private JLabel postalCodeLabel;
private JLabel countryLabel;
private JLabel cityLabel;
private JTextField lastNameField;
private JTextField firstNameField;
private JTextField phoneField;
private JTextField emailField;
private JTextField firstAddressField;
private JTextField secondAddressField;
private JTextField cityField;
private JTextField stateField;
private JTextField postalCodeField;
private JTextField countryField;
private JPanel firstNamePanel;
private JPanel emailPanel;
private JPanel postalCodePanel;
private JPanel buttonsPanel;
private JButton newButton;
private JButton deleteButton;
private JButton editButton;
private JButton saveButton;
private JButton cancelButton;
}
I think the layout usage it's nice and clear. Besides of this, the library has a lot
of functionalities which speeds up the forms development.
|