Created
April 2, 2012 12:05
-
-
Save ashbb/2283029 to your computer and use it in GitHub Desktop.
Clipboard test snippet to get/set text data in JRuby and SWT.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Getting the text data from the system clipboard works well at line 28.
But setting doesn't at line 36. Umm,....