Forum: JRuby Cannot send a Java::int

2a11c93e55ef1e3bcca56c7f2d5e011d?d=identicon&s=25 unknown (Guest)
on 2014-06-25 22:02
(Received via mailing list)
I'm using JRuby and Swing.  For the most part I'm amazed at how easily I
can create and handle my GUI, all from Ruby.  JRuby is great!

In the example below (sorry it's a little long), I see a crash at

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
java.lang.Long cannot be cast to java.lang.Integer

I know that the KeyEvent constant will be converted to a Ruby Fixnum,
which is converted to a Java::long (putValue to MNEMONIC_KEY).  This
needs to be a Java::int, and the mismatch triggers the exception.

*  I've tried the java_send trick, but that second argument to putValue
is a java.lang.Object.

*  I've tried calling to_java(:int) on the KeyEvent constant (or a
variable holding that value), but the behavior is the same.

*  I've tried java_send(:putValue, [java.lang.String, Java::int]), but
then JRuby complains that it can't find that overload -- because it
doesn't exist.

*  I've tried, but this gives me an error.  My bet is
that Java::int is an immediate class that doesn't have a constructor.

*  I've tried, but I get the same

I have a few more variations, but I think you get the idea.  What is the
right way to do this?  Or at this point, is there *any* way to get a
Java::int passed into a java.lang.Object parameter?


========= example code ==============
require 'java'

class Demo
  include_package 'javax.swing'

  def initialize
    # create a menu to hook up to our Action object
    mm ='Hi there')
    mi =
    mbar =

    # create form
    frm = "Simple demo"
    frm.add("Just try to cut some text", 4, 20))
    frm.setDefaultCloseOperation JFrame::EXIT_ON_CLOSE
    frm.visible = true
end # class Demo

class MyAction < javax.swing.AbstractAction
  include_package 'javax.swing'
  include_package 'java.awt.event'

  def initialize
    super("My action")
    self.putValue(SHORT_DESCRIPTION, "Silly dialog box")
KeyStroke.getKeyStroke(KeyEvent::VK_C, ActionEvent::CTRL_MASK))
    self.putValue(Action::MNEMONIC_KEY, KeyEvent::VK_C)

  def actionPerformed(evt)
    JOptionPane.showMessageDialog(null, "My Action", "This is my
action!", JOptionPane::INFORMATION_MESSAGE)
end # class MyAction

javax.swing.SwingUtilities.invokeLater {}
======= end example code ============

Ryan Hinton
L-3 Communications / Communication Systems West
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.