I’m trying to make modal frame in Java Applet such like shown here: http://www.java2s.com/Tutorial/Java/0240__Swing/Showthegivenframeasmodaltothespecifiedowner.htm. This code have start() function, that looks like
public void start() throws Exception {
Class<?> clazz = Class.forName("java.awt.Conditional");
Object conditional = Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz },
this);
Method pumpMethod = Class.forName("java.awt.EventDispatchThread").getDeclaredMethod(
"pumpEvents", new Class[] { clazz });
pumpMethod.setAccessible(true);
pumpMethod.invoke(Thread.currentThread(), new Object[] { conditional });
}.
When I call
pumpMethod.invoke(Thread.currentThread(), new Object[] { conditional });
I have following exception:
java.lang.RuntimeException: java.lang.IllegalArgumentException: object is not an instance of declaring class
at wizard.ModalFrameUtil.showAsModal(ModalFrameUtil.java:136)
at wizard.WizardCore.showWizardFrame(WizardCore.java:206)
at SelfRegistrationApplet$1.run(SelfRegistrationApplet.java:55)
at SelfRegistrationApplet$1.run(SelfRegistrationApplet.java:35)
at java.security.AccessController.doPrivileged(Native Method)
at SelfRegistrationApplet.RunSelfRegistrationApplet(SelfRegistrationApplet.java:32)
at SelfRegistrationApplet.init(SelfRegistrationApplet.java:26)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at wizard.ModalFrameUtil$EventPump.start(ModalFrameUtil.java:80)
at wizard.ModalFrameUtil.showAsModal(ModalFrameUtil.java:133)
... 8 more
Could You please tell what is wrong in this invocation and how to avoid this exception?
What it is saying is that the
Threadobject returned byThread.currentThread()is not an instance ofEventDispatchThread.The way to avoid the problem is to find out what the class of that object really is, and use that class to obtain the
Methodobject. (You should be able to find out what it is by printing the object you get fromThread.currentThread().getClass()at the place where you are trying to invoke the method.The Javadoc for
invokethis says this:My reading of your code is that you have the right number and type of actual arguments, so it must be an issue with the thread class.