11.16 Accessing Components through Id Attributes
- GUI components are associated with application fields that have names
identical to those provided for the id attributes.
- The SwingEngine class provides the following methods for accessing id-ed
GUI components.
- Get the named component.
- Get a java Map of the id-ed components.
- Remove the named component from the map.
- Get an iterator to the id-ed components.
Iterator getIdComponentItertor()
<?xml version="1.0" encoding="UTF-8" ?>
<frame size="300,200" title="id map"
DefaultCloseOperation="JFrame.EXIT_ON_CLOSE">
<panel constraints="BorderLayout.CENTER">
<textfield id="a" text="top" columns="20" />
<panel include="xml/includedxml.xml#b" />
<panel import="xml/importedxml.xml" />
<panel load="xml/loadedxml.xml" />
</panel>
</frame>
-_-_-
<?xml version="1.0" encoding="UTF-8" ?>
<panel id="b">
<textfield id="b.1" text="included" columns="20" />
</panel>
-_-_-
<?xml version="1.0" encoding="UTF-8" ?>
<panel id="c">
<textfield id="c.1" text="imported" columns="20" />
</panel>
-_-_-
<?xml version="1.0" encoding="UTF-8" ?>
<panel id="d">
<textfield id="d.1" text="loaded" columns="20" />
</panel>
-_-_-
Map map = swix.getIdMap();
Set set = map.keySet();
Iterator it = set.iterator();
while( it.hasNext() ){
String key = (String) it.next();
JTextField tf = (JTextField) map.get(key);
System.out.println( key + " " + tf.getText() );
tf.setText( key + ": " + tf.getText() );
}
-_-_-
import javax.swing.*;
import javax.swing.text.AttributeSet;
import org.swixml.SwingEngine;
import java.awt.Container;
import java.io.File;
public class ExternalPanels extends JPanel {
String name=null;
private SwingEngine swix = new SwingEngine (this);
public void setImport (String name) {
this.name=name;
try {
Container jc = swix.render( new File(name) );
add ( jc );
} catch ( Exception e ){ System.out.println( e); }
}
public void setLoad(String filename) {
try {
swix.insert( filename, this );
} catch (Exception e) {
System.err.println( e.getMessage() );
} } }
-_-_-
import javax.swing.*;
import org.swixml.*;
import java.util.*;
public class IdList {
public static void main( String[] args ) throws Exception {
Object obj = new Object();
SwingEngine swix = new SwingEngine( obj );
TagLibrary tl = swix.getTaglib();
tl.unregisterTag("panel");
tl.registerTag("panel", ExternalPanels.class);
swix.render( "xml/topxml.xml" ).setVisible( true );
<.access map ids.>
} } -_-_-
[gui seperation]