JRuby: SwingUtilities#invoke_block_later

5 05 2007

Ruby and Blocks go together like chocolate and peanut butter. With JRuby, you can use blocks to solve old Java problems in new and interesting ways. Earlier this week, while scripting a Swing client, we found that some of our code would generate exceptions from the AWT Thread. I know what you’re thinking: “This sounds like a job for invokeLater.” We thought so too, except, what we really wanted to do was pass a block to invokeLater. Here’s what we came up with:

class BlockRunner < java.lang.Thread
  def initialize(&proc)
    @p = proc
  end
  def run
    @p.call
  end
end

SwingUtils = javax.swing.SwingUtilities

def SwingUtils.invoke_block_later(&proc)
  r = BlockRunner.new &proc
  invoke_later r
end

This code creates BlockRunner, which extends java.lang.Thread. BlockRunner is initialized by passing a block to new. The block is wrapped in a Proc object, which is called when the BlockRunner thread is executed. The SwingUtils#invoke_block_later method instantiates a new BlockRunner, which is passed to #invoke_later.

A call to #invoke_block_later might look like this:

SwingUtils.invoke_block_later do
  some_widget.text = 'blah'
end

Nothing here is particularly earth-shattering. There may even be a better way to this. What’s cool is that, in a very few lines of code, we were able to create an elegant solution to our problem. A solution that looks familiar to Java programmers, while not looking out of place in a Ruby program.

Advertisements

Actions

Information

One response

19 11 2010
roger

This seems more like “invoke block in its own thread” than invokelater…

These days I think you can just call invokeLater directly, like
SwingUtilities.invoke_later {
# stuff
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: