Hi all,
I am busy designing a biggish swing application and I was taken aback
by a cast exception. As I was not expecting an exception I decided to
test this somewhere else.
So I have class (ActionFactory) that extends AbstractAction. The Main
class I am testing from is [PopWindowManager ->calls Test()-> creates a
button and a new action. Then set the action to the button
My code is as follows:
//
//Test ActionFactory
//
private class ActionFactory extends AbstractAction {
/**
* Constructor to create an new action from the gvien parameters.
* @param name
* @param hint
* @param mnemonic
*/
public ActionFactory(String name, String hint, char mnemonic) {
putValue(Action.NAME, name);
putValue(Action.SHORT_DESCRIPTION, hint);
putValue(Action.MNEMONIC_KEY, mnemonic);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Check if this has an exception as well");
}
}
//
//Then I have fucntion to test the action into. Uses a JButton
//
private void test() {
JButton test = new JButton();
ActionFactory factory = new ActionFactory("Test", "Click To
Test", 'T');
test.setAction(factory);
}// test-----------------------------------
//
//Here is the exception
//
Exception in thread "main" java.lang.ClassCastException:
java.lang.Character
at
javax.swing.AbstractButton.configurePropertiesFromAction(AbstractButton.java:11
17)
at
javax.swing.AbstractButton.configurePropertiesFromAction(AbstractButton.java:10
63)
at javax.swing.JButton.configurePropertiesFromAction(JButton.java:220)
at javax.swing.AbstractButton.setAction(AbstractButton.java:997)
at
com.mkhululi.desktion.popup.PopWindowManager.test(PopWindowManager.java:120)
at com.mkhululi.desktion.popup.PopWindowManager.
(PopWindowManager.java:112)
at
com.mkhululi.desktion.popup.PopWindowManager.main(PopWindowManager.java:127)
//------------------------------------------------
PLEASE, help what am I doing wrong!
Thanks in advance!
Yours,
Me
Vova Reznik - 08 Nov 2005 14:28 GMT
> public ActionFactory(String name, String hint, char mnemonic) {
> putValue(Action.NAME, name);
> putValue(Action.SHORT_DESCRIPTION, hint);
> putValue(Action.MNEMONIC_KEY, mnemonic);
public void putValue(String key, Object newValue)
Second parameter should be an object, not primitive
putValue(Action.MNEMONIC_KEY, new Character(mnemonic));
> }
Mkhululi - 08 Nov 2005 14:44 GMT
Thanks a lot. But new Charater does not work either. So I tried new
Integer and things seem fine now.
I will try to find if this is the same silly mistake that causes the
problem in my application.
Thanks again,
Yours,
Me
Vova Reznik - 08 Nov 2005 15:02 GMT
You asked about "Strange Cast Exception".
To set mnemonic read JavaDoc:
"...This method is now obsolete, please use
<code>setMnemonic(int)</code>..."
AbstractButton public void setMnemonic(char mnemonic)
> Thanks a lot. But new Charater does not work either. So I tried new
> Integer and things seem fine now.
Roedy Green - 08 Nov 2005 14:38 GMT
>javax.swing.AbstractButton.configurePropertiesFromAction(AbstractButton.java:11
>17)
>at
presuming you are using JDK 1.5.0_05, the code in question is
if (type.equals(Action.MNEMONIC_KEY)) {
Integer n = (a==null) ? null :
(Integer)a.getValue(type);
setMnemonic(n==null ? '\0' : n.intValue());
No (Character) cast in sight.
So presumably you are using some other JDK. Perhaps you could either
upgrade to the latest JDK and give us the dump, or show us the code
that triggered the cast in your JDK.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 08 Nov 2005 14:44 GMT
>putValue(Action.MNEMONIC_KEY, mnemonic);
You are feeding it a char. Maybe it wants an key-code int or string.
God that kind of code is annoying: defeats the type system, never
properly documented. You see it in JAI, Calendar, Graphics2D...

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.