Tuesday Nov 13, 2007
Tuesday Nov 13, 2007

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
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.
Monday Oct 29, 2007
In this blog, we would like to acknowledge two NetBeans Ruby community-contributed tutorials:
Thanks to Teera and Amit, we have a richer documentation set, which you can view in its entirety on the NetBeans Ruby Tutorials and Screencasts page.
If you are interested in contributing documentation to the NetBeans Community, consider joining the dev@userguide.netbeans.org email alias. This alias is for community members who want to not only contribute documentation but also provide input and feedback regarding existing NetBeans documentation. You can sign up for the alias as follows:
Monday Oct 15, 2007
I have written both an installation and configuration article and an accessing databases FAQ for projects built using NetBeans Ruby support. However, because there are lots of different configuration paths, and each path requires some in-depth information, neither of these documents make it easy to quickly figure out what you must do to set up your Rails project to access your database server. I created this chart to hopefully make the steps simpler, and more clear. If you need more details, then consult the two documents that I mention above.
| Steps for Accessing Databases Using NetBeans Ruby Support | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|||||||||||||||||||||||||
|
|||||||||||||||||||||||||
|
All |
|||||||||||||||||||||||||
| Do: Edit database.yml to specify username, password, and, if necessary, database name. | |||||||||||||||||||||||||
Wednesday Sep 26, 2007
The jMaki 1.0 framework, with support for Java, PHP, and Ruby, was released yesterday. Last week, Arun Gupta posted a screencast showing how to use jMaki wrappers of button and table widgets from Dojo and Yahoo frameworks in a Ruby on Rails application built using the NetBeans 6.0 Beta IDE.
With Arun's permission, I am supplying the steps to recreate a similar jMaki on Rails project. To save steps, I changed the order a bit. If you don't already have NetBeans Ruby Support, go to the Installing and Configuring Ruby Support tutorial for instructions.
mysqladmin -u root -p create rorjmakitables_developmentIf you don't need a password, omit the -p.
development: adapter: mysql database: rorjmakitables_development username: root password: root_password host: localhost
map.connect '', :controller => 'say', :action=>'table'
<%= jmaki_widget 'yahoo.button',
:value => { :label => 'Select 1',
:action => { :topic => '/jmaki/table/select',
:message => { :targetId => '1' }
}
} -%>
<%= jmaki_widget 'yahoo.button',
:value => { :label => 'Select 2',
:action => { :topic => '/jmaki/table/select',
:message => { :targetId => '2' }
}
} -%>
These buttons use the jMaki publish/subscribe mechanism to publish to the /jmaki/table/select topic, which you will program two table widgets to listen to. The table widgets will select either the first row or the second row, depending on which button is clicked. For more details on how to use publish/subscribe, see Carla Mott's Widgets Talking To Widgets blog entry.
<%= jmaki_widget 'yahoo.dataTable', :subscribe=> "/jmaki/table",
:value =>
{:columns => [
{ :label => 'Title', :id => 'title'},
{ :label => 'Author', :id => 'author'},
{ :label => 'ISBN', :id => 'isbn'},
{ :label => 'Description', :id => 'description'}
],
:rows => [
{ :id=> '1', :title => 'Book Title 1',
:author => 'Author 1', :isbn => '4412',
:description => 'A Some long description'},
{ :id => '2', :title => 'Book Title 2',
:author => 'Author 2', :isbn => '4412',
:description => 'A Some long description'}
]
}
-%>
<%= jmaki_widget 'dojo.table', :subscribe=> "/jmaki/table",
:value =>
{:columns => [
{ :label => 'Title', :id => 'title'},
{ :label => 'Author', :id => 'author'},
{ :label => 'ISBN', :id => 'isbn'},
{ :label => 'Description', :id => 'description'}
],
:rows => [
{ :id=> '1', :title => 'Book Title 1',
:author => 'Author 1', :isbn => '4412',
:description => 'A Some long description'},
{ :id=> '2', :title => 'Book Title 2',
:author => 'Author 2', :isbn => '4412',
:description => 'A Some long description'}
]
}
-%>
class CreateGrids < ActiveRecord::Migration
def self.up
create_table :grids do |t|
t.column :title, :string
t.column :author, :string
t.column :isbn, :string
t.column :description, :string
end
Grid.create(:title=> 'Marathon',
:author=> 'Jeff Galloway',
:isbn=> '0936070250',
:description => 'A running book for everybody');
Grid.create(:title=> 'The Runners Bible',
:author=> 'Marc Bloom',
:isbn=> '0385188749',
:description => 'How to train, race, and get in shape');
end
def self.down
drop_table :grids
end
end
class SayController < ApplicationController
def index
table
render :action => 'table'
end
def table
@rows_data = []
Grid.find_all().each do |data|
@rows_data << { :id => data.id,
:title => data.title,
:author => data.author,
:isbn => data.isbn,
:description => data.description
}
end
end
end
The above code defines the rows_data variable and initializes it to an empty array. It uses the active record to find all the rows from the database and, for each row, add a hash of the fields to the rows_data array.
<%= jmaki_widget 'dojo.table',
:value =>
{:columns => [
{ :label => 'Title', :id => 'title'},
{ :label =>'Author', :id => 'author'},
{ :label => 'ISBN', :id => 'isbn'},
{ :label => 'Description', :id => 'description'}
],
:rows => @rows_data
}
-%>
<%= jmaki_widget 'yahoo.dataTable',
:value =>
{:columns => [
{ :label => 'Title', :id => 'title'},
{ :label => 'Author', :id => 'author'},
{ :label => 'ISBN', :id => 'isbn'},
{ :label => 'Description', :id => 'description'}
],
:rows => @rows_data
}
-%>
There you have it! To learn more about jMaki, go to www.jmaki.com. To learn more about NetBeans support for Ruby, go to wiki.netbeans.org/wiki/view/Ruby.
Monday Sep 17, 2007
Last week, Arun Gupta paid a visit to the NetBeans Ruby writers. It was great to finally meet this dynamo in person and find out what a nice guy he is. When we first started writing about NetBeans Ruby, we used Arun's blog Miles to Go as one of our resources to quickly learn about the technology. Arun tells us that the blog's name has a double meaning: he is both an avid runner and he feels like he has a lot to learn in his job.
In addition to Ruby/JRuby and GlassFish, Arun blogs on a variety of topics, including jMaki, web services, WSIT, Ajax, and Web 2.0. Arun somehow manages to be one of the first to write about how to use emerging technologies, so we like to keep an eye on his blog to find out what we will be writing about in the future. For example, last Friday, he blogged about a new GlassFish gem for Rails. We also like to follow his popular "Tip of the Day" entries. You will often see his information folded into our tutorials. Arun seems to really understand his audience's information needs and he has been very helpful by providing good suggestions as to what to include in our tutorials.
Wednesday Sep 12, 2007
There is a rumor floating around that the NetBeans IDE Ruby support only works for JRuby. Don't believe it. The IDE makes it very easy to use your own Ruby installation in addition to the bundled JRuby software. As a matter of fact, the first time that you open or create a Ruby project, the IDE looks on your system for other Ruby installations. If it finds any, it pops up a dialog box listing the available installations and lets you choose which one to use.
Using JRuby is just like using Ruby, and you don't have to know anything about Java to use JRuby. You can open your existing Ruby applications for development in the IDE and work with them using the IDE's features regardless of whether you have chosen to use the JRuby interpreter or your native Ruby interpreter. The one exception that I know of is that with JRuby you have to use the ActiveRecord-JDBC adapter if you are using a database server other than MySQL. However, all you have to do is to put the client driver in JRuby's lib folder, make a couple of simple changes to your database.yml, and add a snippet to your environment.rb. Tom Enebro writes about it here. The ActiveRecord-JDBC adapter works with MySQL, Postgresql, Oracle, Derby/Java DB, HQLSDB, and H2 database management systems.
One of the advantages of using JRuby is that you can access Java libraries from your Ruby application. Tor and Cindy put together a great video about this for NetBeans TV.
Another benefit is that you can war up a JRuby application and deploy it to a Java application server, such as GlassFish, just like you would with a Java web application. Arun Gupta has written several blog entries about this and the upcoming Installing and Setting Up Ruby tutorial provides step-by-step instructions for one of the simpler methods of deploying to the GlassFish server. One advantage of deploying to Java application servers is that they are designed to be multi-threaded and thus can handle more than one request at a time. In addition, Java application servers provide tools that make it easier to manage your deployed applications. For detailed information about creating and deploying JRuby applications, see the Rails Integration page on the JRuby wiki.