001: /*
002: * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.forms.tutorial;
032:
033: import javax.swing.JComponent;
034: import javax.swing.JFrame;
035: import javax.swing.JTextField;
036: import javax.swing.UIManager;
037: import javax.swing.WindowConstants;
038:
039: import com.jgoodies.forms.builder.PanelBuilder;
040: import com.jgoodies.forms.layout.CellConstraints;
041: import com.jgoodies.forms.layout.FormLayout;
042:
043: /**
044: * Quickly introduces the most important features of the FormLayout:
045: * create and configure a layout, create a builder, add components.<p>
046: *
047: * Note that this class is not a JPanel subclass;
048: * it justs uses a JPanel as layout container that will be returned
049: * by <code>#buildPanel()</code>.
050: *
051: * @author Karsten Lentzsch
052: * @version $Revision: 1.13 $
053: */
054:
055: public final class QuickStartExample {
056:
057: private JTextField companyField;
058: private JTextField contactField;
059: private JTextField ptiField;
060: private JTextField powerField;
061: private JTextField radiusField;
062: private JTextField diameterField;
063:
064: public static void main(String[] args) {
065: try {
066: UIManager
067: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
068: } catch (Exception e) {
069: // Likely PlasticXP is not in the class path; ignore.
070: }
071: JFrame frame = new JFrame();
072: frame.setTitle("Forms Tutorial :: Quick Start");
073: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
074: JComponent panel = new QuickStartExample().buildPanel();
075: frame.getContentPane().add(panel);
076: frame.pack();
077: frame.setVisible(true);
078: }
079:
080: // Component Creation and Initialization **********************************
081:
082: /**
083: * Creates, intializes and configures the UI components.
084: * Real applications may further bind the components to underlying models.
085: */
086: private void initComponents() {
087: companyField = new JTextField();
088: contactField = new JTextField();
089: ptiField = new JTextField(6);
090: powerField = new JTextField(10);
091: radiusField = new JTextField(8);
092: diameterField = new JTextField(8);
093: }
094:
095: // Building *************************************************************
096:
097: /**
098: * Builds the panel. Initializes and configures components first,
099: * then creates a FormLayout, configures the layout, creates a builder,
100: * sets a border, and finally adds the components.
101: *
102: * @return the built panel
103: */
104: public JComponent buildPanel() {
105: // Separating the component initialization and configuration
106: // from the layout code makes both parts easier to read.
107: initComponents();
108:
109: // Create a FormLayout instance on the given column and row specs.
110: // For almost all forms you specify the columns; sometimes rows are
111: // created dynamically. In this case the labels are right aligned.
112: FormLayout layout = new FormLayout(
113: "right:pref, 3dlu, pref, 7dlu, right:pref, 3dlu, pref", // cols
114: "p, 3dlu, p, 3dlu, p, 9dlu, p, 3dlu, p, 3dlu, p"); // rows
115:
116: // Specify that columns 1 & 5 as well as 3 & 7 have equal widths.
117: layout.setColumnGroups(new int[][] { { 1, 5 }, { 3, 7 } });
118:
119: // Create a builder that assists in adding components to the container.
120: // Wrap the panel with a standardized border.
121: PanelBuilder builder = new PanelBuilder(layout);
122: builder.setDefaultDialogBorder();
123:
124: // Obtain a reusable constraints object to place components in the grid.
125: CellConstraints cc = new CellConstraints();
126:
127: // Fill the grid with components; the builder offers to create
128: // frequently used components, e.g. separators and labels.
129:
130: // Add a titled separator to cell (1, 1) that spans 7 columns.
131: builder.addSeparator("General", cc.xyw(1, 1, 7));
132: builder.addLabel("Company", cc.xy(1, 3));
133: builder.add(companyField, cc.xyw(3, 3, 5));
134: builder.addLabel("Contact", cc.xy(1, 5));
135: builder.add(contactField, cc.xyw(3, 5, 5));
136:
137: builder.addSeparator("Propeller", cc.xyw(1, 7, 7));
138: builder.addLabel("PTI/kW", cc.xy(1, 9));
139: builder.add(ptiField, cc.xy(3, 9));
140: builder.addLabel("Power/kW", cc.xy(5, 9));
141: builder.add(powerField, cc.xy(7, 9));
142: builder.addLabel("R/mm", cc.xy(1, 11));
143: builder.add(radiusField, cc.xy(3, 11));
144: builder.addLabel("D/mm", cc.xy(5, 11));
145: builder.add(diameterField, cc.xy(7, 11));
146:
147: // The builder holds the layout container that we now return.
148: return builder.getPanel();
149: }
150:
151: }
|