Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2005

Tip: Looking for answers? Try searching our database.

Strange Cast Exception - AbstractAction

Thread view: 
Mkhululi - 08 Nov 2005 13:41 GMT
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.