Tuesday Nov 13, 2007

Getting Your Feet Wet With Java in JRuby

If you haven't tried calling Java classes from a JRuby application yet, here is a simple code snippet to get you started. Paste the following code into the Ruby shell (JRuby IRB), press Enter, and a small desktop app opens (To open the Ruby shell in the NetBeans IDE, choose Ruby > Other > Ruby Shell (IRB) from the main menu). This code is an updated version of an example in the JRuby and the Java Platform article.

Note: A current known bug causes the JRuby IRB to report an ExitSecurityException, this bug does not affect the output.

Code Sample: Using Java Classes in the IRB Console
include Java

import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.JButton
import java.awt.BorderLayout

frame = JFrame.new
panel = JPanel.new
panel.layout = BorderLayout.new
panel.background = java.awt.Color::white
frame.get_content_pane.add(panel)
frame.default_close_operation = JFrame::EXIT_ON_CLOSE
button = JButton.new "Click Me"
text = JLabel.new "I'm a Simple Program"

panel.add(BorderLayout::CENTER, text)
panel.add(BorderLayout::SOUTH, button)

class Click
    include java.awt.event.ActionListener

    def initialize(button, text)
        @button, @text = button, text
        @click_me_mode = true
    end

    def actionPerformed(event)
        source = event.source
        if (source == @button)
            if (@click_me_mode)
                @text.text = "Button Clicked"
                @button.text = "Click Again"
                @click_me_mode = false
            else
                @text.text = "I'm a Simple Program"
                @button.text = "Click Me"
                @click_me_mode = true
            end
        end
    end
end

button.add_action_listener(Click.new(button, text))
frame.title = "Example"
frame.pack
frame.visible = true 

The IRB is a nice way to test out your code. However, with the NetBeans IDE, it is just as easy to test out code in a scratch program. To see what I mean, right-click in the NetBeans Projects window and choose New > Project. In the New Project wizard, select Ruby in the Categories pane, select Ruby Application in the Projects pane, and click Next. Name the project Scratch (or whatever you want) and click Finish. The main.rb file opens in the editor.

Replace the contents of the main.rb file with the code sample. Then click the Run Main Project button in the main toolbar (the green arrow) to run the application.

As you can see by the following screenshot, the advantage of testing your code in a scratch project as opposed to using the IRB is that you get syntax coloring and all the other wonderful NetBeans Ruby editing features.

To learn about the editing features, see the NetBeans Ruby Editing wiki page, or look at Tor's screenshot of the week entries, such as Ruby Screenshot of the Week #18: Errors and Snippets (as of today, he is up to screenshot #23). We are also working on a getting started guide that will cover some of the editing features. Last, you can watch Roman's Editing screencast.

There are a couple of FAQs that you might find helpful

  1. How to Call Java From Ruby
  2. How Can I Access Java From the JRuby IRB?

You also might want to check out the Swing with JRuby: Developing a Desktop Application with the JRuby and Java Swing APIs tutorial that was written by Sun campus ambassador Teera Kanokkanjanarat.

About

divas

Search

Archives
« July 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today