Skip to content

Instantly share code, notes, and snippets.

@ashbb
Created April 2, 2012 12:05
Show Gist options
  • Save ashbb/2283029 to your computer and use it in GitHub Desktop.
Save ashbb/2283029 to your computer and use it in GitHub Desktop.
Clipboard test snippet to get/set text data in JRuby and SWT.
require 'java'
require 'swt'
module Swt
include_package 'org.eclipse.swt'
include_package 'org.eclipse.swt.widgets'
include_package 'org.eclipse.swt.dnd'
import java.awt.datatransfer.StringSelection
import java.awt.Toolkit
end
class Object
include Swt
end
display = Swt::Display.new
shell = Swt::Shell.new display
shell.setSize 300+16, 300+38
cs = Swt::Composite.new shell, Swt::SWT::NONE
cs.setSize 300, 300
cb_get = Swt::Clipboard.new display
cb_set = Swt::Toolkit.getDefaultToolkit.getSystemClipboard
tt = Swt::TextTransfer.getInstance
b = Swt::Button.new cs, Swt::SWT::NULL
b.setText 'get'
b.setLocation 50, 50
b.pack
b.addSelectionListener do
p cb_get.getContents(tt) # Works well!
end
b = Swt::Button.new cs, Swt::SWT::NULL
b.setText 'set'
b.setLocation 200, 50
b.pack
b.addSelectionListener do
# cb.setContents(['hello hello hello'], tt) # NameError... why?
cb_set.setContents Swt::StringSelection.new('hello hello hello'), self # Works well! Thanks, Roger!!
end
shell.open
while !shell.isDisposed do
display.sleep unless display.readAndDispatch
end
display.dispose
@ashbb
Copy link
Author

ashbb commented Apr 12, 2012

Got a solution! Thanks to Roger!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment