Strange Cast Exception - AbstractAction

Hi all,

NOTE: for some strange reason - - does not format my code.

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->callsTest()-> creates a button and a new action. Then set the action to the button

My code is as follows:

//

//Test ActionFactory

//

privateclass ActionFactoryextends 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);

}

publicvoid 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

//

privatevoid 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

NOTE: for some strange reason - - does not format my code.

[3265 byte] By [Khusta] at [2007-11-25 20:44:59]
# 1

This is because Action.MNEMONIC_KEY needs an int not a char. (see javax.swing.Action)

so you must rewrite :

private class ActionFactory extends AbstractAction {

public ActionFactory(String name, String hint, [b]int[/b] mnemonic) {

...

}

...

ActionFactory factory = new ActionFactory("Test", "Click To Test", [b]KeyEvent.VK_T[/b]);

lysandro at 2007-7-4 18:27:18 > top of Java-index,Desktop,Sun Java Desktop System...
# 2
Thanks a lot. Another entry of this problem can be found at: http://groups.google.co.za/group/comp.lang.java.gui/browse_thread/thread/98d877 ef290b227a/601b56b49d90343c
Khusta at 2007-7-4 18:27:18 > top of Java-index,Desktop,Sun Java Desktop System...