|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.swing.LayoutUtils
public class LayoutUtils
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).
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 |
|---|
public LayoutUtils()
| Method Detail |
|---|
public static JPanel createLabeledFields(String[] labels,
JComponent[] fields)
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;
}
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).
panel with your labels and fields installed.
public static JPanel createLabeledFields(ResourceBundle rb,
String id,
String sep,
JComponent[] fields)
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:
ResourceBundle - allowing one to
localize their application.
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;
}
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).
panel with your labels and fields installed.
public static JPanel createLabeledFields(TagLookup tl,
String sep,
JComponent[] fields)
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:
ResourceBundle - allowing one to
localize their application.
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;
}
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).
panel with your labels and fields installed.
public static Component searchForComponentByName(Container top,
String name,
boolean recurse)
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.
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.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||