A
Alexander Burger
Hi all,
can anybody explain why the getMethod() call (in the line commented with
"???" below) throws a NoSuchMethodException?
Perhaps I'm doing something obvious wrong, but I couldn't find any hint.
################################################################
import javax.swing.*;
import java.lang.reflect.*;
public class F {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("Title");
JPanel panel = (JPanel)frame.getContentPane();
JTextArea area = new JTextArea(10, 40);
Method method;
frame.setSize(300, 200);
frame.setLocation(200, 200);
// This works ('add' JTextArea to JPanel):
panel.add(area);
// This, however, does not work (same 'add' JTextArea to JPanel):
// ??? method = panel.getClass().getMethod("add", area.getClass());
// This again works:
method = panel.getClass().getMethod("setName", "newName".getClass());
frame.setVisible(true);
}
}
################################################################
The direct call
panel.add(area);
works, but the corresponding getMethod()
method = panel.getClass().getMethod("add", area.getClass());
throws
NoSuchMethodException: javax.swing.JPanel.add(javax.swing.JTextArea)
getMethod() on another method like "setName" with a string type works.
Both methods - add() and setName() - should be inherited from the
superclasses of JPanel. They are 'public' in Container and/or Component.
What is the difference, or what am I doing wrong?
Cheers,
- Alex
can anybody explain why the getMethod() call (in the line commented with
"???" below) throws a NoSuchMethodException?
Perhaps I'm doing something obvious wrong, but I couldn't find any hint.
################################################################
import javax.swing.*;
import java.lang.reflect.*;
public class F {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("Title");
JPanel panel = (JPanel)frame.getContentPane();
JTextArea area = new JTextArea(10, 40);
Method method;
frame.setSize(300, 200);
frame.setLocation(200, 200);
// This works ('add' JTextArea to JPanel):
panel.add(area);
// This, however, does not work (same 'add' JTextArea to JPanel):
// ??? method = panel.getClass().getMethod("add", area.getClass());
// This again works:
method = panel.getClass().getMethod("setName", "newName".getClass());
frame.setVisible(true);
}
}
################################################################
The direct call
panel.add(area);
works, but the corresponding getMethod()
method = panel.getClass().getMethod("add", area.getClass());
throws
NoSuchMethodException: javax.swing.JPanel.add(javax.swing.JTextArea)
getMethod() on another method like "setName" with a string type works.
Both methods - add() and setName() - should be inherited from the
superclasses of JPanel. They are 'public' in Container and/or Component.
What is the difference, or what am I doing wrong?
Cheers,
- Alex