JAXX file

From JAXXWiki

elttrrobolet JAXX files are XML files with the extension .jaxx. A JAXX file is similar to a Java file in that it represents a single top-level class, and the source file's path and name are significant (corresponding to the package and class names).

Contents

Class and Package Names

The name of a compiled JAXX class is the name of its .jaxx file, minus the extension. Its package name is derived from the path, very similarly to the way Java class and package naming works.

Unlike Java files, the class and package name are not specified in the JAXX file itself. JAXX is entirely dependent on the path used as an argument to jaxxc, which means that you must be careful to compile the class file using the desired package path.

jaxxc examples/Simple.jaxx will create a Java class named examples.Simple
cd examples; jaxxc Simple.jaxx will create a Java class named Simple

Simple JAXX File

<Application title='Simple JAXX File'>
  <JButton label='Close' onActionPerformed='dispose()'/>
</Application>

The root tag is a class tag, in this case <Application> (which is a subclass of javax.swing.JFrame). The class compiled from a JAXX file will be a subclass of its root tag. Because the name of the compiled class is derived from the path and filename of the JAXX file, this means we are compiling a class named examples.Simple which extends Application.

The root tag has a child tag, <JButton>, which is another class tag. Because the <JButton> tag is a child of the <Application> tag, the corresponding JButton component will likewise be a child of the Application component. The XML attributes title and label correspond to the Application.setTitle() and JButton.setLabel() methods, respectively.

The onActionPerfomed attribute instructs JAXX to create an ActionListener which will run the script dispose() in its actionPerformed() method.

This JAXX file is equivalent to the following Java code:

package examples;

import javax.swing.*;

public class Simple extends jaxx.runtime.swing.Application {
    public Simple() {
        setTitle("Simple JAXX File");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JButton button = new JButton();
        button.setLabel("Close");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        getContentPane.add(button);

        pack();
    }


    public static void main(String[] arg) {
        new Simple().setVisible(true);
    }
}

The actual compiled Java code is somewhat more complicated than this (it is machine-generated code, after all) but semantically equivalent. Note that the compiled code includes a reference to the JFrame.pack() method and a main() method, neither of which were mentioned in the JAXX file: these are both created by the <Application> tag.

Root Tag

As with all XML files, JAXX files contain a single root tag. This tag must be a class tag, and the JAXX class will be a subclass of the root tag class. Abstract root tags are legal, but interfaces are not. There is currently no way to indicate that the JAXX class should implement interfaces, but this is easily worked around by creating a Java class which implements the interfaces and then using that as a root tag. There are plans to fix the interface limitations in the future.

The compiled JAXX class will have a no-argument constructor, in addition to any constructors that may be specified within script tags. This no-argument constructor will perform all of the object's setup, including calling set methods to apply attributes, creating children, adding event listeners, and running scripts.

Child Tags

For specific details on what can and cannot appear within a given tag, please see the documentation for the specific tag.

In general, though, most JAXX tags represent subclasses of java.awt.Container and follow similar rules. Most containers permit three kinds of children:

  • Components, which are added to the container. Layout manager constraints may be specified using the constraints attribute.
  • Classes other than components, which are not added to the container and may not have constraints specified. Generally, to be useful these tags must have an id attribute so that they can be referenced from scripts or data binding expressions.
  • Script tags, which are Java code snippets that are inlined into the compiled class.

IDs

You can associate a name with an object by specifying an id attribute on a class tag. This id can then be used to refer to the object from anywhere else in the file.

Example:

<JFrame title='ID Example'>
  <JLabel id='label' text='Button not clicked' constraints='BorderLayout.NORTH'/>
  <JButton id='button' label='Click Me' 
           onActionPerformed='label.setText("Button clicked!")' 
           constraints='BorderLayout.SOUTH'/>
</JFrame>

The id label assigns a name to the JLabel, and this name can then be referred to from within the onActionPerformed event handler.

Overrides

JAXX files which subclass other JAXX files may override class tags defined in their parents by using the same id attribute, similarly to the way methods may be overridden by using the same names and argument types. See Overriding class tags for more details.