ID (attribute)

From JAXXWiki

Class tags support a special id attribute to associate a name with the object. An object's id can be used to reference it elsewhere in the JAXX file.

ID Example

examples/IDExample.jaxx

<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.

How IDs Work

In technical terms, the id attribute causes a protected field to be created. The Java code created by the example above is equivalent to:

package examples;

public class IDExample extends JFrame {
    protected JLabel label;

    public IDExample() {
        setTitle("ID Example");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        createLabel();
        getContentPane().add(label, BorderLayout.NORTH);

        JButton button = new JButton();
        button.setLabel("Click Me");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                label.setText("Button clicked!");
            }
        });
        getContentPane().add(label, BorderLayout.SOUTH);

        pack();
    }


    protected void createLabel() {
        label = new JLabel();
        label.setText("Button not clicked");
    }


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

Because the JLabel is held in a field named label, this means that scripts (such as the JButton's event handler) can easily reference it using its id.

Overriding by ID

When a JAXX class extends another JAXX class, the subclass can replace objects from its superclass by using tags with the same ID. See overriding class tags for details.