com.ccg.swing
Class LayoutUtils

java.lang.Object
  extended by com.ccg.swing.LayoutUtils

public class LayoutUtils
extends Object

General methods that help with the laying out of components.

This class provides some generic methods to help one lay out components. Methods which work with resource bundles allow one to perform the layouts using localized text (making it easier to port your application to different languages).

Since:
1.0
Version:
$Revision: 1.5 $
Author:
$Author: pkb $
See Also:
Resources

Constructor Summary
LayoutUtils()
           
 
Method Summary
static JPanel createLabeledFields(ResourceBundle rb, String id, String sep, JComponent[] fields)
          Line up labels with text (or other components) using a resource bundle.
static JPanel createLabeledFields(String[] labels, JComponent[] fields)
          Line up labels with text (or other components).
static JPanel createLabeledFields(TagLookup tl, String sep, JComponent[] fields)
          Line up labels with text (or other components) using a TagLookup.
static Component searchForComponentByName(Container top, String name, boolean recurse)
          Search (optionally recursively) for a component with a particular name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LayoutUtils

public LayoutUtils()
Method Detail

createLabeledFields

public static JPanel createLabeledFields(String[] labels,
                                         JComponent[] fields)
Line up labels with text (or other components).

This method is a slightly modified version from the "Trail: Creating a GUI with JFC/Swing" tutorial. It allows one to nicely line up and associate labels with corresponding components. It makes the following assumptions:

Here is a quick example:


 private JTextField _Text;
 
 private JCheckBox _Choice;
 
 private JButton _Button;
 
 private JComponent _All;
 
 public Component getComponent() {
        if (_All == null) {
                String[] labels = { "Name:", "Hungry?:", "Pizza Fetch:" };
                JComponent[] fields = { _Text = new JTextField("Frankie"),
                                _Choice = new JCheckBox(), _Button = new JButton("Order") };
                _All = LayoutUtils.createLabeledFields(labels, fields);
        }
        return _All;
 }
 
 

Parameters:
labels - Array of strings to label each component with (you can pass null, have null entries in your array, of not match the length of your fields - we'll deal with it as best we can).
arg2 - Array of components corresponding to the labels (you can pass null, have null entries in your array, of not match the length of your labels - we'll deal with it as best we can).
Returns:
A panel with your labels and fields installed.
Since:
1.0

createLabeledFields

public static JPanel createLabeledFields(ResourceBundle rb,
                                         String id,
                                         String sep,
                                         JComponent[] fields)
Line up labels with text (or other components) using a resource bundle.

This method provides a handy means to label components using a resource bundle. It builds upon the simpler implementation. It adds the following features:

You need to use this method in conjunction with a resource bundle. The following shows a section of a resource bundle that could be used with this method:

         po.title=Pizza Order Properties
         po.width=7
         po.labels=Name:,Hungry?:,Pizza Fetch:
 

Assuming the above resources where available in the passed ResourceBundle the following demonstrates how this method could be used:


 private JTextField _Text;
 
 private JCheckBox _Choice;
 
 private JButton _Button;
 
 private JComponent _All;
 
 public Component getComponent(ResourceBundle rb) {
        if (_All == null) {
                JComponent[] fields = { _Text = new JTextField("Frankie"),
                                _Choice = new JCheckBox(), _Button = new JButton("Order") };
                _All = LayoutUtils.createLabeledFields(rb, "po", null, fields);
        }
        return _All;
 }
 
 

Parameters:
rb - The resource bundle to fetch string values from (it shouldn't be null - but we handle it by omitting the labels in the returned component).
id - The ID which will be used to look for text strings that we use to create the panel.
sep - The separator(s) used to distinguish labels in the "ID.labels" string. If you pass null, the default separator characters of ",\n\t" will be used when parsing the labels.
fields - Array of components corresponding to the labels (you can pass null, have null entries in your array, of not match the length of your labels - we'll deal with it as best we can).
Returns:
A panel with your labels and fields installed.
Since:
1.0

createLabeledFields

public static JPanel createLabeledFields(TagLookup tl,
                                         String sep,
                                         JComponent[] fields)
Line up labels with text (or other components) using a TagLookup.

This method provides a handy means to label components using a resource bundle. It builds upon the simpler implementation. It adds the following features:

You need to use this method in conjunction with a tagged lookup object. The following shows a section of a resource bundle that could be used with this method:

         po.title=Pizza Order Properties
         po.width=7
         po.labels=Name:,Hungry?:,Pizza Fetch:
 

Assuming the above resources where available in the passed TagLookup object. The following demonstrates how this method could be used:


 private JTextField _Text;
 
 private JCheckBox _Choice;
 
 private JButton _Button;
 
 private JComponent _All;
 
 public Component getComponent(TagLookup tl) {
        if (_All == null) {
                JComponent[] fields = { _Text = new JTextField("Frankie"),
                                _Choice = new JCheckBox(), _Button = new JButton("Order") };
                _All = LayoutUtils.createLabeledFields(tl, null, fields);
        }
        return _All;
 }
 
 

Parameters:
tl - The tagged lookup object to fetch string values from (it shouldn't be null - but we handle it by omitting the labels in the returned component).
id - The ID which will be used to look for text strings that we use to create the panel.
sep - The separator(s) used to distinguish labels in the "ID.labels" string. If you pass null, the default separator characters of ",\n\t" will be used when parsing the labels.
fields - Array of components corresponding to the labels (you can pass null, have null entries in your array, of not match the length of your labels - we'll deal with it as best we can).
Returns:
A panel with your labels and fields installed.
Since:
1.0

searchForComponentByName

public static Component searchForComponentByName(Container top,
                                                 String name,
                                                 boolean recurse)
Search (optionally recursively) for a component with a particular name.

This static method is used to search for a component with a particular name. It will search the entire heirarchy (recursively) if so instructed. It returns the first component found which has the specified name or null if the component is not found.

Parameters:
c - Starting point of search (you can pass null in which case we return null).
name - Name of the component to locate (you can pass null in which case we return null).
recurse - Whether a exhaustive (recursive search) should be performed.
Returns:
First component found with matching name (or null if not found).
Since:
1.0


Copyright 1998-1998-2006 null. All Rights Reserved.