public class UIFragment extends Object
BorderLayout and BoxLayout include some variant tags to customize their behaviour also:
<border behavior='absolute'/>
<border behavior='totalBelow'/>
<x noGrow='true'/>
Component.getUIID()
findById(java.lang.String)
Component.getName()
<x>
only. Specifies that BoxLayout should be X_AXIS_NO_GROW. Accepts values "true" and "false".<y>
only. Specifies that BoxLayout should use Y_AXIS_BOTTOM_LAST option. Accepts values "true" and "false"
Form f = new Form("Test", new BorderLayout());
String tpl = "<border>"
+ "<border constraint='center'><border constraint='south'><$button constraint='east'/></border></border>"
+ "<$search constraint='south'/>"
+ "</border>";
f.setFormBottomPaddingEditingMode(true);
TextField searchField = new TextField();
searchField.addActionListener(e->{
Log.p("Search field action performed");
});
Button submit = new Button("Submit");
submit.addActionListener(e->{
Log.p("Button action performed");
});
UIFragment template = UIFragment.parseXML(tpl)
.set("button", submit)
.set("search", searchField);
f.add(BorderLayout.CENTER, template.getView());
f.show();
When trying to express a UI structure inside a Java String, the XML notation may be a little bit verbose and cumbersome. For this reason, there is an alternative JSON-based notation that will allow you to express a UI structure more succinctly.
A JSON object (i.e. curly braces) denotes a Container. If this object includes properties
corresponding to the constraints of BorderLayout
(e.g. center, east, west, north, south, or overlay), then
the container will use a BorderLayout
, and the associated properties will represent its children
with the appropriate constraint.
E.g.:
{center:'Center Label', south:'South Content'}
Will create a Container with labels in its BorderLayout.CENTER
and BorderLayout.SOUTH
positions.
To make things even more succinct, it supports single-character property keys for the BorderLayout constraint values. E.g. The following is equivalent to the previous example:
{c:'Center Label', s:'South Content'}
Other Layouts:
{flow:[...]}
{grid:[...], cols:3, rows:2}
{x:[...]}
{y:[...]}
{layered:[...]}
{table:[['A1', 'B1', 'C1'], ['A2', 'B2', 'C2'], ...]}
Layout Variants
BoxLayout and BorderLayout include variant shorthands to customize their behaviour.
{x:[...], noGrow:true}
{y:[...], bottomLast:true}
{center:[...], behavior:absolute}
{center:[...], behavior:totalBelow}
Embedding Placeholders/Parameters
The notation for embedding placeholder components (which must be injected via set(java.lang.String, com.codename1.ui.Component)
),
is similar to the XML equivalent. Just place the parmeter name, prefixed with '$'. E.g.
$submitButton
Component view = UIFragment.parseJSON("{n:['Hello', 'World', $checkbox], c:[y, {class:'MyTable', table:[['Username', $username], ['Password', $password]]}, {flow:['Some text'], align:center}], s:$submit}")
.set("username", new TextField())
.set("password", new TextField())
.set("submit", new Button("Submit"))
.set("checkbox", new CheckBox("Check Me"))
.getView();
Modifier and Type | Class and Description |
---|---|
static interface |
UIFragment.ComponentFactory
A factory for converting XML elements to Components.
|
static class |
UIFragment.DefaultComponentFactory
Default component factory that is used in templates.
|
Modifier and Type | Method and Description |
---|---|
Component |
findById(String id)
Gets a component in the template by its ID.
|
UIFragment.ComponentFactory |
getFactory()
Gets the component factory that is currently set for this fragment.
|
Container |
getView()
Gets the view that is generated by this template.
|
static UIFragment |
parseJSON(String json)
Parses a JSON string into a template.
|
static UIFragment |
parseXML(InputStream input)
Parses input stream of XML into a Template
|
static UIFragment |
parseXML(String xml)
Parses XML string into a Template
|
UIFragment |
set(String paramName,
Component param)
Sets a parameter component in this template.
|
UIFragment |
setFactory(UIFragment.ComponentFactory factory)
Sets the component factory to be used.
|
public static UIFragment parseXML(InputStream input)
input
- InputStream with XML content to parsepublic static UIFragment parseXML(String xml)
xml
- XML string describing a UI.public static UIFragment parseJSON(String json)
json
- A JSON string representing a UI hierarchy.IOException
public Container getView()
public UIFragment set(String paramName, Component param)
paramName
- The name of the parameter.param
- The component to inject into the template.public Component findById(String id)
id
- The ID of the component.public UIFragment.ComponentFactory getFactory()
public UIFragment setFactory(UIFragment.ComponentFactory factory)
factory
- the factory to set