OnJava8-Examples/ui/ShowAddListeners.java

63 lines
1.9 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// ui/ShowAddListeners.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-06-15 17:47:35 -07:00
// Display the "addXXXListener" methods of any Swing class.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.*;
import java.util.regex.*;
import static onjava.SwingConsole.*;
2015-06-15 17:47:35 -07:00
public class ShowAddListeners extends JFrame {
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 {
@Override
public void actionPerformed(ActionEvent e) {
String nm = name.getText().trim();
if(nm.length() == 0) {
results.setText("No match");
return;
}
Class<?> kind;
try {
kind = Class.forName("javax.swing." + nm);
} catch(ClassNotFoundException ex) {
results.setText("No match");
return;
}
Method[] methods = kind.getMethods();
results.setText("");
for(Method m : methods) {
Matcher matcher =
addListener.matcher(m.toString());
if(matcher.find())
results.append(qualifier.matcher(
matcher.group(1)).replaceAll("") + "\n");
}
}
}
public ShowAddListeners() {
NameL nameListener = new NameL();
name.addActionListener(nameListener);
JPanel top = new JPanel();
top.add(new JLabel("Swing class name (press Enter):"));
top.add(name);
add(BorderLayout.NORTH, top);
add(new JScrollPane(results));
// Initial data and test:
name.setText("JTextArea");
nameListener.actionPerformed(
2015-11-03 12:00:44 -08:00
new ActionEvent("", 0, ""));
2015-06-15 17:47:35 -07:00
}
public static void main(String[] args) {
run(new ShowAddListeners(), 500, 400);
}
2015-09-07 11:44:36 -06:00
}