public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setText("SWT and Swing DND Example"); GridLayout layout = new GridLayout(1, false); shell.setLayout(layout); Text swtText = new Text(shell, SWT.BORDER); swtText.setText("SWT Text"); GridData data = new GridData(GridData.FILL_HORIZONTAL); swtText.setLayoutData(data); setDragDrop(swtText); Composite comp = new Composite(shell, SWT.EMBEDDED); java.awt.Frame frame = SWT_AWT.new_Frame(comp); JTextField swingText = new JTextField(40); swingText.setText("Swing Text"); swingText.setDragEnabled(true); frame.add(swingText); data = new GridData(GridData.FILL_HORIZONTAL); data.heightHint = swingText.getPreferredSize().height; comp.setLayoutData(data); shell.setSize(400, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
void createRTFTransfer(Composite copyParent, Composite pasteParent) { // RTF Transfer Label l = new Label(copyParent, SWT.NONE); l.setText("RTFTransfer:"); // $NON-NLS-1$ copyRtfText = new Text(copyParent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); copyRtfText.setText("some\nrtf\ntext"); GridData data = new GridData(GridData.FILL_HORIZONTAL); data.heightHint = data.widthHint = SIZE; copyRtfText.setLayoutData(data); Button b = new Button(copyParent, SWT.PUSH); b.setText("Copy"); b.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { String data = copyRtfText.getText(); if (data.length() > 0) { status.setText(""); data = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\uc1\\b\\i " + data + "}"; clipboard.setContents( new Object[] {data}, new Transfer[] {RTFTransfer.getInstance()}); } else { status.setText("nothing to copy"); } } }); l = new Label(pasteParent, SWT.NONE); l.setText("RTFTransfer:"); // $NON-NLS-1$ pasteRtfText = new Text(pasteParent, SWT.READ_ONLY | SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); data = new GridData(GridData.FILL_HORIZONTAL); data.heightHint = data.widthHint = SIZE; pasteRtfText.setLayoutData(data); b = new Button(pasteParent, SWT.PUSH); b.setText("Paste"); b.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { String data = (String) clipboard.getContents(RTFTransfer.getInstance()); if (data != null && data.length() > 0) { status.setText(""); pasteRtfText.setText("start paste>" + data + "<end paste"); } else { status.setText("nothing to paste"); } } }); }
void createHTMLTransfer(Composite copyParent, Composite pasteParent) { // HTML Transfer Label l = new Label(copyParent, SWT.NONE); l.setText("HTMLTransfer:"); // $NON-NLS-1$ copyHtmlText = new Text(copyParent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); copyHtmlText.setText("<b>Hello World</b>"); GridData data = new GridData(GridData.FILL_HORIZONTAL); data.heightHint = data.widthHint = SIZE; copyHtmlText.setLayoutData(data); Button b = new Button(copyParent, SWT.PUSH); b.setText("Copy"); b.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { String data = copyHtmlText.getText(); if (data.length() > 0) { status.setText(""); clipboard.setContents( new Object[] {data}, new Transfer[] {HTMLTransfer.getInstance()}); } else { status.setText("nothing to copy"); } } }); l = new Label(pasteParent, SWT.NONE); l.setText("HTMLTransfer:"); // $NON-NLS-1$ pasteHtmlText = new Text(pasteParent, SWT.READ_ONLY | SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); data = new GridData(GridData.FILL_HORIZONTAL); data.heightHint = data.widthHint = SIZE; pasteHtmlText.setLayoutData(data); b = new Button(pasteParent, SWT.PUSH); b.setText("Paste"); b.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { String data = (String) clipboard.getContents(HTMLTransfer.getInstance()); if (data != null && data.length() > 0) { status.setText(""); pasteHtmlText.setText("start paste>" + data + "<end paste"); } else { status.setText("nothing to paste"); } } }); }
void createControlTransfer(Composite parent) { Label l = new Label(parent, SWT.NONE); l.setText("Text:"); Button b = new Button(parent, SWT.PUSH); b.setText("Cut"); b.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { text.cut(); } }); b = new Button(parent, SWT.PUSH); b.setText("Copy"); b.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { text.copy(); } }); b = new Button(parent, SWT.PUSH); b.setText("Paste"); b.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { text.paste(); } }); text = new Text(parent, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); GridData data = new GridData(GridData.FILL_HORIZONTAL); data.heightHint = data.widthHint = SIZE; text.setLayoutData(data); l = new Label(parent, SWT.NONE); l.setText("Combo:"); b = new Button(parent, SWT.PUSH); b.setText("Cut"); b.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { combo.cut(); } }); b = new Button(parent, SWT.PUSH); b.setText("Copy"); b.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { combo.copy(); } }); b = new Button(parent, SWT.PUSH); b.setText("Paste"); b.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { combo.paste(); } }); combo = new Combo(parent, SWT.NONE); combo.setItems(new String[] {"Item 1", "Item 2", "Item 3", "A longer Item"}); l = new Label(parent, SWT.NONE); l.setText("StyledText:"); l = new Label(parent, SWT.NONE); l.setVisible(false); b = new Button(parent, SWT.PUSH); b.setText("Copy"); b.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { styledText.copy(); } }); b = new Button(parent, SWT.PUSH); b.setText("Paste"); b.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { styledText.paste(); } }); styledText = new StyledText(parent, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); data = new GridData(GridData.FILL_HORIZONTAL); data.heightHint = data.widthHint = SIZE; styledText.setLayoutData(data); }
public static void main(String[] args) { final Display display = new Display(); final Clipboard clipboard = new Clipboard(display); final Shell shell = new Shell(display, SWT.SHELL_TRIM); shell.setLayout(new GridLayout()); shell.setText("Clipboard ImageTransfer"); final Button imageButton = new Button(shell, SWT.NONE); GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true); gd.minimumHeight = 400; gd.minimumWidth = 600; imageButton.setLayoutData(gd); final Text imageText = new Text(shell, SWT.BORDER); imageText.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false)); Composite buttons = new Composite(shell, SWT.NONE); buttons.setLayout(new GridLayout(4, true)); Button button = new Button(buttons, SWT.PUSH); button.setText("Open"); button.addListener( SWT.Selection, event -> { FileDialog dialog = new FileDialog(shell, SWT.OPEN); dialog.setText("Open an image file or cancel"); String string = dialog.open(); if (string != null) { imageButton.setText(""); Image image = imageButton.getImage(); if (image != null) image.dispose(); image = new Image(display, string); imageButton.setImage(image); imageText.setText(string); } }); button = new Button(buttons, SWT.PUSH); button.setText("Copy"); button.addListener( SWT.Selection, event -> { Image image = imageButton.getImage(); if (image != null) { ImageTransfer imageTransfer = ImageTransfer.getInstance(); TextTransfer textTransfer = TextTransfer.getInstance(); clipboard.setContents( new Object[] {image.getImageData(), imageText.getText()}, new Transfer[] {imageTransfer, textTransfer}); } }); button = new Button(buttons, SWT.PUSH); button.setText("Paste"); button.addListener( SWT.Selection, event -> { ImageData imageData = (ImageData) clipboard.getContents(ImageTransfer.getInstance()); if (imageData != null) { imageButton.setText(""); Image image = imageButton.getImage(); if (image != null) image.dispose(); image = new Image(display, imageData); imageButton.setImage(image); } else { imageButton.setText("No image"); imageButton.setImage(null); } String text = (String) clipboard.getContents(TextTransfer.getInstance()); if (text != null) { imageText.setText(text); } else { imageText.setText(""); } }); button = new Button(buttons, SWT.PUSH); button.setText("Clear"); button.addListener( SWT.Selection, event -> { imageButton.setText(""); Image image = imageButton.getImage(); if (image != null) image.dispose(); imageButton.setImage(null); imageText.setText(""); }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }