//: c14:ShowAddListeners.java // Display the "addXXXListener" methods of any Swing class. // <applet code=ShowAddListeners // width=500 height=400></applet> import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.lang.reflect.*; import java.util.regex.*; import com.bruceeckel.swing.*; public class ShowAddListeners extends JApplet { private JTextField name = new JTextField(25); private JTextArea results = new JTextArea(40, 65); private static Pattern addListener = Pattern.compile("(add\\w+?Listener\\(.*?\\))"); private static Pattern qualifier = Pattern.compile("\\w+\\."); class NameL implements ActionListener { public void actionPerformed(ActionEvent e) { String nm = name.getText().trim(); if(nm.length() == 0) { results.setText("No match"); return; } Class klass; try { klass = Class.forName("javax.swing." + nm); } catch(ClassNotFoundException ex) { results.setText("No match"); return; } Method[] methods = klass.getMethods(); results.setText(""); for(int i = 0; i < methods.length; i++) { Matcher matcher = addListener.matcher(methods[i].toString()); if(matcher.find()) results.append(qualifier.matcher( matcher.group(1)).replaceAll("") + "\n"); } } } public void init() { NameL nameListener = new NameL(); name.addActionListener(nameListener); JPanel top = new JPanel(); top.add(new JLabel("Swing class name (press ENTER):")); top.add(name); Container cp = getContentPane(); cp.add(BorderLayout.NORTH, top); cp.add(new JScrollPane(results)); // Initial data and test: name.setText("JTextArea"); nameListener.actionPerformed( new ActionEvent("", 0 ,"")); } public static void main(String[] args) { Console.run(new ShowAddListeners(), 500,400); } } ///:~