Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7064255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:48:43+00:00 2026-05-28T04:48:43+00:00

I am trying to recreate SwiXML examples in JRuby. But the objects created in

  • 0

I am trying to recreate SwiXML examples in JRuby. But the objects
created in JRuby never seem to be visible to SwiXML. Here is an example.

<frame size="200,200" title="Action Test">
  <menubar>
    <menu text="File">
      <menuitem action="quit" accelerator="meta X" />
    </menu>
  </menubar>
  <button action="quit" text="A Quit Button"
  ToolTipText="This is a quit button." />
</frame>

The Java code from the SwiXML example is as follows:

public class ActionTest {
  // Create an Action as a member variable
  public Action quit = new AbstractAction() {
    public void actionPerformed(ActionEvent evt) {
      System.exit(0);
    }
  };
  public ActionTest() throws Exception {
    // set the Action's name
    quit.putValue(Action.NAME, "Quit");
    // build the GUI
    new SwingEngine(this).render("ActionTest.xml")
    .setVisible(true);
  }
  public static void main(String[] args) throws Exception {
    new ActionTest();
  }
}

I have created some JRuby code to correspond to this, but it seems as if
the @quit member is never seen. Also tried referencing other named
elements (not in this example):

require 'java'
require 'java/swixml.jar'
require 'java/jdom.jar'

include_class 'javax.swing.AbstractAction'
include_class 'javax.swing.Action'
include_class 'javax.swing.JButton'

class MyAction < AbstractAction
  def actionPerformed(ae)
    exit # puts "Clicked"
  end
end

class Main < Object # Java::JavaLang::Object
  def initialize
    @quit = MyAction.new
    @quit.putValue(Action.NAME, "Quit")
    @f = java.io.File.new("sample.xml")
    @se = org.swixml.SwingEngine.new(self).render(@f).setVisible(true)
  end
end

Main.new
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:48:43+00:00Added an answer on May 28, 2026 at 4:48 am

    I’ve been struggling with integrating JRuby and SwiXml this week. I’ve come to the conclusion that you can’t have SwiXml automatically bind your variables/actions from the XML. (I think this is because in Java the variables already exist, whereas in JRuby they are created ‘on-the-fly’, so SwiXml isn’t sure what to do. That’s my conclusion, anyway, after hours of digging through source code. JRuby is fairly new to me, so someone more advanced might be able to tell me why this won’t work.)

    The solution is to simply bind them manually in the JRuby code. It’s actually fairly easy, since this is Ruby.

    include Java
    java_import 'org.swixml.SwingEngine'
    
    class HelloWorld
    
      def initialize
        @swix = SwingEngine.new(self)
        @swix.render("xml/helloworld.xml")
        btn = @swix.find("submit")
        btn.add_action_listener { |event| btn.text = 'Hi' }
      end
    
      def run
        @swix.getRootComponent.setVisible(true)
      end
    
    end
    

    See? Not too bad. In my case, “submit” is defined as the <button>‘s ID attribute. So in my XML file I have <button text="Click Here" id="submit" /> Think of the find() method like a findById() method (if you’re familiar with DOM manipulation through JavaScript…).

    Note that since the add_action_listener takes a block, instance variables (ivars) can be included in the block (in other words, it acts like you would expect a Java anonymous class/block to work). There’s other ways to implement/add an action listener. See this page: https://github.com/jruby/jruby/wiki/FAQs and scroll down to the section with a heading that says How can I implement a Java Interface using a Ruby Class?

    Any element (as far as I know), can be retrieved by the SwingEngine class’s find() method, as long as it’s id is defined in the XML file.

    A few minor things: include_class is now deprecated. You should use java_import instead. Also, your class that you’re passing to SwingEngine does not need to inherit from Object or anything like that. JRuby is getting much better about making things more ‘ruby-like’ when integrating with Java.

    Hope this helps. There’s not much info out there about this stuff.

    P.S. I found the info about ‘manually binding’ from this link: http://www.mail-archive.com/forum@carlsbadcubes.com/msg00062.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to recreate a modernized version of this tutorial here - but I'm
i'm trying to recreate the MVC model of their simple example but i'm having
In this small, but full example, I am trying to recreate a dual list
I am trying to recreate the FragmentPagerSupport example but I keep getting an error
this may sound funny, but i spent hours trying to recreate a knob with
I'm trying to recreate a sort of table layout of a contact form. Here's
I'm trying to recreate this UISearchBar (as seen in the Table Search example code):
I'm trying to recreate the 'Overlay and Shadow Classes' effect used here: http://jqueryui.com/themeroller/ with
Im trying to recreate this page http://psdtowp.com/order-now but for another purpose, Im very new
I am trying to recreate an example from Chapter-5 of book Pro Asp.net MVC2.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.