XmlPanel

Miscellaneous Items

There are a few conveniences that have been added to the XmlPanel to make development a little easier. This section describes some of these "shortcuts" that have been added.

Getting Components By Name

Every java.awt.Component has a name property. Components can be given names, and there is no requirement to make them unique. Giving a component a name can help with being able to locate that component.

Consider the following XML that creates a panel with a single button in it. Notice that the name can be set in the XML, since it is just a simple setter method on the object:

<panel>
    <row>
        <cell type="button" name="MyButton">Press This</cell>
    </row>
</panel>

This code can be used to setup the panel and all is fine. That is, until the developer needs to get at the button to programmatically do something with it. For example, there may come a point in the application where the button should be set as disabled. How can a handle to the button object currently in the panel be retrieved?

To help with these scenarios, the XmlPanel has a getComponentNamed(String) method. Given the name of the component (case-sensitive), the first object found inside of the panel with that name will be returned. Using this method, the following code snippet could be used to get the button out of the panel and set it to be disabled:

Component comp = panel.getComponentNamed("MyButton");
comp.setEnabled(false);

While this example is simplistic, the concept becomes useful with larger and deeply nested panels. Components will be found in the root panel and any subpanel inside of it.

ActionListener Panel

XmlPanel extends ActionListenerJPanel. This specialized subclass of JPanel allows an ActionListener to be installed on the panel. Any buttons that are added to the panel, or any subpanel inside of it, will trigger ActionListeners on the XmlPanel.

This allows action listeners to be installed directly on the panel, and then are not needed on the buttons themselves. This allows a central working place for actions that can handle all of the buttons on the panel. See the ActionListenerJPanel JavaDoc for more information.