« Return to Thread: Java integration: Swing string problem
Hi,
I'm mainly just recording some information here in a place where it
might be useful. I can't drill deeper at the moment. There's no bug
here, but there may be an opportunity to improve Java integration.
I encountered a case where Swing compares a passed in string, against
a static final field, using ==. IOW, they're using it like an enum. In
Java SE 6 it's in AbstractButton.actionPropertyChanged.
So my JRuby program calls AbstractAction putValue(String key, object
value). The action object notifies listeners, including JMenuItems
which inherit from AbstractButton.
This is how how you change menu text in Swing, so the undo menu reads
"Undo Typing" or whatever.
My call in jruby looked like: my_action.put_value(Action::NAME, "Undo Typing")
The AbstractButton code, however, fails the comparison of this key
against Action.NAME. I guess JRuby is passing a copy of the string
around. That's why it fails ==.
My workaround is:
ActionNameKey =
javax.swing.Action.java_class.declared_field('NAME').static_value
my_action.put_value(ActionNameKey, "Undo Typing")
If I had the time right now I'd dig into whether Jruby could make
cases like this succeed by always calling String.intern before passing
the string around. 1. I don't know that jruby doesn't do this, 2. I
don't know that it would fix the problem. But it would be nice to
reduce the friction if possible.
Hugh
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email
« Return to Thread: Java integration: Swing string problem
| Free embeddable forum powered by Nabble | Forum Help |