Class tag |
From JAXXWiki
A class tag is a tag that creates an instance of a Java class. Most tags are class tags; there are only a few exceptions (such as [[Script (ta 2000 g)|<script>]] ), all of which by convention start with lowercase letters to distinguish them from the normally-uppercase class tags.
Contents |
Creating class tags
To create a class tag, just use the name of the Java class you are trying to create:
<JButton/>
This creates a new instance of the javax.swing.JButton class, using its default constructor. You do not have to specify the package name in this case because javax.swing is imported by default. To reference classes from another package, you may either specify the full name of the class (<java.awt.Button/>) or import the class using a script(<script>import java.awt.*;</script> ... <Button />).
Just as in Java, the current package is imported by default and therefore two classes in the same package can reference each other via their simple, unqualified names. java.lang.*, javax.swing.*, and the JAXX-specific HBox, VBox, and Table classes are also imported by default.
What class tags do
A class tag creates a new instance of the specified class, generally via its no-argument constructor. If the class is a Component and its parent tag is a Container, the component is added to the container. If the tag has an id attribute, the object can be referenced from elsewhere in the file by using the id.
Components are generally useful whether or not they have an ID, as they inserted into the user interface regardless. Non-component objects are seldom useful without an ID because there is no way to refer to them. If a non-component tag does not have an ID, JAXX "drops it on the floor" (does not retain a reference to it) after creating it. This usually causes it to be immediately garbage collected.
The exact behavior of a class tag depends upon the particular class being referenced (or, more precisely, the TagHandler associated with it). Almost all classes simply use DefaultObjectHandler or < 2000
- tt>DefaultComponentHandler</tt>, and the few that don't instead use subclasses with only slight changes. The documentation on DefaultObjectHandler contains more details, but in general
- The id attribute associates a name with the object instance
- The constraints attribute specifies layout constraints
- The constructorParams attribute gives parameters to the object's constructor
- The styleClass attribute specifies the object's CSS style class
- Other attributes (e.g. text) change the object's properties using setter methods (setText())
- Event handler attributes (on followed by a capital letter, e.g. onActionPerformed) create event handlers
Importing other JAXX files
Because a JAXX file is a Java class, JAXX files can load other JAXX files by using class tags, just as they would any other Java class. If the referenced JAXX file is newer than the corresponding .class file (or there is no corresponding .class file), the JAXX file will be recompiled.
Example:
[examples/Frame.jaxx] <JFrame title="Frame"> <RedLabel text="This label is red!"/> </JFrame> [examples/RedLabel.jaxx] <JLabel foreground="red"/>
Specifying class names
As mentioned above, classes can be referenced via either their simple or fully-qualified names. Because JAXX is XML, it also permits package names to be specified as XML namespaces. The following examples all refer to the class java.awt.Button:
<java.awt.Button/>
<script>import java.awt.*;</script> <Button/>
<Button xmlns="java.awt.*"/>
<awt:Button xmlns:awt="java.awt.*"/>
<java.awt.Button xmlns="java.awt.*"/>
<!-- Note that the namespace is javax.swing!
This sets the *default* package to javax.swing,
but then overrides it on the tag itself -->
<java.awt.Button xmlns="javax.swing.*"/>
<!-- There is no need to specify the package name
twice, but it is not an error -->
<awt:java.awt.Button xmlns:awt="java.awt.*"/>
As illustrated above, the package name can be specified directly, implied due to an import, specified directly using a namespace prefix, or implied due to a default namespace. It is an error to directly specify a package name using both a tag and a namespace prefix if the two package names do not match:
<!-- this is an error, as we have specified
two mismatching package names -->
<swing:java.awt.Button xmlns:swing="javax.swing.*"/>

