Jump to: navigation, search

Difference between revisions of "JRuby/SWTSetup"

 
Line 17: Line 17:
 
</pre>
 
</pre>
  
* try some SWT expressions interactively in jirb:  
+
* try some SWT expressions interactively in jirb (see note below first if running on Mac):  
 
<pre>
 
<pre>
 
jirb
 
jirb

Latest revision as of 08:19, 28 July 2009

To set up JRuby with SWT:

  • download JRuby
  • extract it somewhere, for example: c:\jruby (on Windows), ~/jruby (on Linux or Mac), or /usr/lib/jruby (on Linux or Mac, but this will likely require admin privileges)
  • download the standalone SWT (Mac users may grab either the Cocoa or Carbon version of SWT, but you know you want Cocoa)
  • extract it somewhere else
  • copy swt.jar to the jruby lib directory, for example: /usr/lib/jruby/lib
  • add the jruby bin directory to your path, for example:
    • on Windows: set PATH=c:\jruby\bin;%PATH%
    • on Linux or Mac: export PATH=/usr/lib/jruby/bin:$PATH
  • ensure jruby runs:
jruby -v

should produce something like:

jruby 1.3.1 (ruby 1.8.6p287) (2009-06-15 2fd6c3d) (Java HotSpot(TM) Client VM 1.5.0_16) [i386-java]
  • try some SWT expressions interactively in jirb (see note below first if running on Mac):
jirb
irb(main):001:0> display = org.eclipse.swt.widgets.Display.getDefault
irb(main):002:0> shell = org.eclipse.swt.widgets.Shell.new(display)
irb(main):003:0> shell.setText("Hi there")                           
irb(main):004:0> shell.open                                          
irb(main):005:0> shell.dispose

Note that JRuby lets you write using Java bean conventions, for example: shell.text="foo" is equivalent to shell.setText("foo"), and Display.default is equivalent to Display.default, so the above could also be written:

irb(main):001:0> display = org.eclipse.swt.widgets.Display.default
irb(main):002:0> shell = org.eclipse.swt.widgets.Shell.new(display)
irb(main):003:0> shell.text="Hi there"
irb(main):004:0> shell.open
irb(main):005:0> shell.dispose

To spin the SWT event loop:

irb(main):006:0> display.sleep unless display.readAndDispatch until shell.isDisposed

To exit jirb:

irb(main):007:0> exit

Note that when running on the Mac, the SWT event loop must run on the first thread of the process, otherwise you'll get the spinning beachball and the app will be unresponsive (and will have to be killed with Force Quit). This is also true for jirb. To do this, specify the -XstartOnFirstThread=true JVM argument as follows:

jruby -J-XstartOnFirstThread Snippet1.rb

or you can do:

export JAVA_OPTS=-XstartOnFirstThread
jruby Snippet1.rb

Unfortunately, jirb does not recognize the -J option, so only the latter method works with jirb.