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 drawItem(GC gc, boolean drawFocus) { int headerHeight = parent.getBandHeight(); Display display = getDisplay(); gc.setForeground(display.getSystemColor(SWT.COLOR_TITLE_BACKGROUND)); gc.setBackground(display.getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT)); gc.fillGradientRectangle(x, y, width, headerHeight, true); if (expanded) { gc.setForeground(display.getSystemColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT)); gc.drawLine(x, y + headerHeight, x, y + headerHeight + height - 1); gc.drawLine(x, y + headerHeight + height - 1, x + width - 1, y + headerHeight + height - 1); gc.drawLine(x + width - 1, y + headerHeight + height - 1, x + width - 1, y + headerHeight); } int drawX = x; if (image != null) { drawX += ExpandItem.TEXT_INSET; if (imageHeight > headerHeight) { gc.drawImage(image, drawX, y + headerHeight - imageHeight); } else { gc.drawImage(image, drawX, y + (headerHeight - imageHeight) / 2); } drawX += imageWidth; } if (text.length() > 0) { drawX += ExpandItem.TEXT_INSET; Point size = gc.stringExtent(text); gc.setForeground(parent.getForeground()); gc.drawString(text, drawX, y + (headerHeight - size.y) / 2, true); } int chevronSize = ExpandItem.CHEVRON_SIZE; drawChevron(gc, x + width - chevronSize, y + (headerHeight - chevronSize) / 2); if (drawFocus) { gc.drawFocus(x + 1, y + 1, width - 2, headerHeight - 2); } }
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new RowLayout()); Combo combo = new Combo(shell, SWT.NONE); combo.setItems(new String[] {"A-1", "B-1", "C-1"}); Text text = new Text(shell, SWT.SINGLE | SWT.BORDER); text.setText("some text"); combo.addListener( SWT.DefaultSelection, new Listener() { public void handleEvent(Event e) { System.out.println(e.widget + " - Default Selection"); } }); text.addListener( SWT.DefaultSelection, new Listener() { public void handleEvent(Event e) { System.out.println(e.widget + " - Default Selection"); } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("StyledText with underline and strike through"); shell.setLayout(new FillLayout()); StyledText text = new StyledText(shell, SWT.BORDER); text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ"); // make 0123456789 appear underlined StyleRange style1 = new StyleRange(); style1.start = 0; style1.length = 10; style1.underline = true; text.setStyleRange(style1); // make ABCDEFGHIJKLM have a strike through StyleRange style2 = new StyleRange(); style2.start = 11; style2.length = 13; style2.strikeout = true; text.setStyleRange(style2); // make NOPQRSTUVWXYZ appear underlined and have a strike through StyleRange style3 = new StyleRange(); style3.start = 25; style3.length = 13; style3.underline = true; style3.strikeout = true; text.setStyleRange(style3); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
static boolean runLoopTimer(final Display display, final Shell shell, final int seconds) { final boolean[] timeout = {false}; new Thread() { public void run() { try { for (int i = 0; i < seconds; i++) { Thread.sleep(1000); if (display.isDisposed() || shell.isDisposed()) return; } } catch (Exception e) { } timeout[0] = true; /* wake up the event loop */ if (!display.isDisposed()) { display.asyncExec( new Runnable() { public void run() { if (!shell.isDisposed()) shell.redraw(); } }); } } }.start(); while (!timeout[0] && !shell.isDisposed()) if (!display.readAndDispatch()) display.sleep(); return timeout[0]; }
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new RowLayout()); Text text = new Text(shell, SWT.MULTI | SWT.BORDER); String modifier = SWT.MOD1 == SWT.CTRL ? "Ctrl" : "Command"; text.setText("Hit " + modifier + "+Return\nto see\nthe default button\nrun"); text.addTraverseListener( e -> { switch (e.detail) { case SWT.TRAVERSE_RETURN: if ((e.stateMask & SWT.MOD1) != 0) e.doit = true; } }); Button button = new Button(shell, SWT.PUSH); button.pack(); button.setText("OK"); button.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { System.out.println("OK selected"); } }); shell.setDefaultButton(button); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
public void positionWindow(boolean resetToDefault) { String mapWindow; mapShellBounds = mapShell.getBounds(); mapWindow = prefs.get(RadarConsts.PREF_KEY_MAP_WINDOW, ""); if (mapWindow.equals("") || resetToDefault) { Rectangle dispBounds, tableBounds; dispBounds = ((Display.getCurrent()).getPrimaryMonitor()).getClientArea(); tableBounds = display.getShells()[1].getBounds(); mapShellBounds.width = dispBounds.width - tableBounds.width; mapShellBounds.height = dispBounds.height; mapShellBounds.x = dispBounds.x + tableBounds.width; mapShellBounds.y = dispBounds.y; } else { String[] tokens = mapWindow.split(","); mapShellBounds = new Rectangle( new Integer(tokens[0]), // X value new Integer(tokens[1]), // Y value new Integer(tokens[2]), // Width new Integer(tokens[3]) // Height ); } mapShell.setBounds(mapShellBounds); }
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new RowLayout()); DateTime calendar = new DateTime(shell, SWT.CALENDAR); calendar.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { System.out.println("calendar date changed"); } }); DateTime time = new DateTime(shell, SWT.TIME); time.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { System.out.println("time changed"); } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout()); final ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1)); Composite c = new Composite(sc, SWT.NONE); c.setLayout(new GridLayout(10, true)); for (int i = 0; i < 300; i++) { Button b = new Button(c, SWT.PUSH); b.setText("Button " + i); } sc.setContent(c); sc.setExpandHorizontal(true); sc.setExpandVertical(true); sc.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT)); sc.setShowFocusedControl(true); shell.setSize(300, 500); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
static long /*int*/ MenuItemSelectedProc(long /*int*/ widget, long /*int*/ user_data) { Display display = Display.getCurrent(); ToolItem item = (ToolItem) display.getWidget(user_data); if (item != null) { return item.getParent().menuItemSelected(widget, item); } return 0; }
public static void main(String[] args) { Display display = new Display(); AddressBook application = new AddressBook(); Shell shell = application.open(display); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
/** * Constructs a new instance of this class given the display to create it on and a style value * describing its behavior and appearance. * * <p>The style value is either one of the style constants defined in class <code>SWT</code> which * is applicable to instances of this class, or must be built by <em>bitwise OR</em>'ing together * (that is, using the <code>int</code> "|" operator) two or more of those <code>SWT</code> style * constants. The class description lists the style constants that are applicable to the class. * Style bits are also inherited from superclasses. * * <p>Note: Currently, null can be passed in for the display argument. This has the effect of * creating the tracker on the currently active display if there is one. If there is no current * display, the tracker is created on a "default" display. <b>Passing in null as the display * argument is not considered to be good coding style, and may not be supported in a future * release of SWT.</b> * * @param display the display to create the tracker on * @param style the style of control to construct * @exception SWTException * <ul> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass * </ul> * * @see SWT#LEFT * @see SWT#RIGHT * @see SWT#UP * @see SWT#DOWN */ public Tracker(Display display, int style) { if (display == null) display = Display.getCurrent(); if (display == null) display = Display.getDefault(); if (!display.isValidThread()) { error(SWT.ERROR_THREAD_INVALID_ACCESS); } this.style = checkStyle(style); this.display = display; }
private static void start() { Display display = new Display(); Shell shell = new Shell(display); MainPane mainPane = new MainPane(shell, SWT.NONE); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
public static void main(String[] args) { Display display = new Display(); MessageWindow application = new MessageWindow(); Shell shell = application.open(display); if (args.length != 0) { application.openAddressBook(args[0]); } while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); final Button button = new Button(shell, SWT.NONE); button.setSize(100, 100); button.setText("Click"); shell.pack(); shell.open(); button.addListener( SWT.MouseDown, new Listener() { @Override public void handleEvent(Event e) { System.out.println( "Mouse Down (button: " + e.button + " x: " + e.x + " y: " + e.y + ")"); } }); final Point pt = display.map(shell, null, 50, 50); new Thread() { Event event; @Override public void run() { try { Thread.sleep(300); } catch (InterruptedException e) { } event = new Event(); event.type = SWT.MouseMove; event.x = pt.x; event.y = pt.y; display.post(event); try { Thread.sleep(300); } catch (InterruptedException e) { } event.type = SWT.MouseDown; event.button = 1; display.post(event); try { Thread.sleep(300); } catch (InterruptedException e) { } event.type = SWT.MouseUp; display.post(event); } }.start(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
protected String[] getAuthenticationDialog(final String realm, final String tracker) { final Display display = SWTThread.getInstance().getDisplay(); if (display.isDisposed()) { return (null); } final AESemaphore sem = new AESemaphore("SWTAuth"); final authDialog[] dialog = new authDialog[1]; TOTorrent torrent = TorrentUtils.getTLSTorrent(); final String torrent_name; if (torrent == null) { torrent_name = null; } else { torrent_name = TorrentUtils.getLocalisedName(torrent); } try { display.asyncExec( new AERunnable() { public void runSupport() { dialog[0] = new authDialog(sem, display, realm, tracker, torrent_name); } }); } catch (Throwable e) { Debug.printStackTrace(e); return (null); } sem.reserve(); String user = dialog[0].getUsername(); String pw = dialog[0].getPassword(); String persist = dialog[0].savePassword() ? "true" : "false"; if (user == null) { return (null); } return (new String[] {user, pw == null ? "" : pw, persist}); }
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final Table table = new Table(shell, SWT.BORDER); table.setHeaderVisible(true); final TableColumn column1 = new TableColumn(table, SWT.NONE); column1.setText("Column 1"); final TableColumn column2 = new TableColumn(table, SWT.NONE); column2.setText("Column 2"); TableItem item = new TableItem(table, SWT.NONE); item.setText(new String[] {"a", "3"}); item = new TableItem(table, SWT.NONE); item.setText(new String[] {"b", "2"}); item = new TableItem(table, SWT.NONE); item.setText(new String[] {"c", "1"}); column1.setWidth(100); column2.setWidth(100); Listener sortListener = e -> { TableItem[] items = table.getItems(); Collator collator = Collator.getInstance(Locale.getDefault()); TableColumn column = (TableColumn) e.widget; int index = column == column1 ? 0 : 1; for (int i = 1; i < items.length; i++) { String value1 = items[i].getText(index); for (int j = 0; j < i; j++) { String value2 = items[j].getText(index); if (collator.compare(value1, value2) < 0) { String[] values = {items[i].getText(0), items[i].getText(1)}; items[i].dispose(); TableItem item1 = new TableItem(table, SWT.NONE, j); item1.setText(values); items = table.getItems(); break; } } } table.setSortColumn(column); }; column1.addListener(SWT.Selection, sortListener); column2.addListener(SWT.Selection, sortListener); table.setSortColumn(column1); table.setSortDirection(SWT.UP); shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
/** Invokes as a standalone program. */ public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display, SWT.SHELL_TRIM); shell.setLayout(new FillLayout()); ControlExample instance = new ControlExample(shell); shell.setText(getResourceString("window.title")); setShellSize(instance, shell); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } instance.dispose(); display.dispose(); }
boolean fillAccel(ACCEL accel) { accel.cmd = accel.key = accel.fVirt = 0; if (accelerator == 0 || !getEnabled()) return false; if ((accelerator & SWT.COMMAND) != 0) return false; int fVirt = OS.FVIRTKEY; int key = accelerator & SWT.KEY_MASK; int vKey = Display.untranslateKey(key); if (vKey != 0) { key = vKey; } else { switch (key) { /* * Bug in Windows. For some reason, VkKeyScan * fails to map ESC to VK_ESCAPE and DEL to * VK_DELETE. The fix is to map these keys * as a special case. */ case 27: key = OS.VK_ESCAPE; break; case 127: key = OS.VK_DELETE; break; default: { key = Display.wcsToMbcs((char) key); if (key == 0) return false; if (OS.IsWinCE) { key = (int) /*64*/ OS.CharUpper((short) key); } else { vKey = OS.VkKeyScan((short) key); if (vKey == -1) { if (key != (int) /*64*/ OS.CharUpper((short) key)) { fVirt = 0; } } else { key = vKey & 0xFF; } } } } } accel.key = (short) key; accel.cmd = (short) id; accel.fVirt = (byte) fVirt; if ((accelerator & SWT.ALT) != 0) accel.fVirt |= OS.FALT; if ((accelerator & SWT.SHIFT) != 0) accel.fVirt |= OS.FSHIFT; if ((accelerator & SWT.CONTROL) != 0) accel.fVirt |= OS.FCONTROL; return true; }
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout(2, true)); final Browser browser; try { browser = new Browser(shell, SWT.NONE); } catch (SWTError e) { System.out.println("Could not instantiate Browser: " + e.getMessage()); display.dispose(); return; } GridData data = new GridData(GridData.FILL_BOTH); data.horizontalSpan = 2; browser.setLayoutData(data); Button headersButton = new Button(shell, SWT.PUSH); headersButton.setText("Send custom headers"); data = new GridData(); data.horizontalAlignment = GridData.FILL; headersButton.setLayoutData(data); headersButton.addListener( SWT.Selection, event -> browser.setUrl( "http://www.ericgiguere.com/tools/http-header-viewer.html", null, new String[] {"User-agent: SWT Browser", "Custom-header: this is just a demo"})); Button postDataButton = new Button(shell, SWT.PUSH); postDataButton.setText("Send post data"); data = new GridData(); data.horizontalAlignment = GridData.FILL; postDataButton.setLayoutData(data); postDataButton.addListener( SWT.Selection, event -> browser.setUrl( "https://bugs.eclipse.org/bugs/buglist.cgi", "emailassigned_to1=1&bug_severity=enhancement&bug_status=NEW&email1=platform-swt-inbox&emailtype1=substring", null)); shell.setBounds(10, 10, 600, 600); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
public static void main(String[] args) { Display display = Display.getDefault(); Shell shell = new Shell(display); @SuppressWarnings("unused") MainWindow inst = new MainWindow(shell, SWT.NULL); shell.setLayout(new FillLayout()); shell.setImage(SWTResourceManager.getImage("images/16x16.png")); shell.setText("Change This Title"); shell.setBackgroundImage(SWTResourceManager.getImage("images/ToolbarBackground.gif")); shell.layout(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } }
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); shell.setText("Snippet 346"); Text text = new Text(shell, SWT.PASSWORD | SWT.BORDER); text.setTextChars(new char[] {'p', 'a', 's', 's'}); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
/** * Sets the image the receiver will display to the argument. * * <p>Note: This operation is a hint and is not supported on platforms that do not have this * concept (for example, Windows NT). Furthermore, some platforms (such as GTK), cannot display * both a check box and an image at the same time. Instead, they hide the image and display the * check box. * * @param image the image to display * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> */ public void setImage(Image image) { checkWidget(); if ((style & SWT.SEPARATOR) != 0) return; super.setImage(image); if (OS.IsWinCE) { if ((OS.IsPPC || OS.IsSP) && parent.hwndCB != 0) { long /*int*/ hwndCB = parent.hwndCB; TBBUTTONINFO info = new TBBUTTONINFO(); info.cbSize = TBBUTTONINFO.sizeof; info.dwMask = OS.TBIF_IMAGE; info.iImage = parent.imageIndex(image); OS.SendMessage(hwndCB, OS.TB_SETBUTTONINFO, id, info); } return; } if (OS.WIN32_VERSION < OS.VERSION(4, 10)) return; MENUITEMINFO info = new MENUITEMINFO(); info.cbSize = MENUITEMINFO.sizeof; info.fMask = OS.MIIM_BITMAP; if (parent.foreground != -1) { info.hbmpItem = OS.HBMMENU_CALLBACK; } else { if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION(6, 0) && OS.IsAppThemed()) { if (hBitmap != 0) OS.DeleteObject(hBitmap); info.hbmpItem = hBitmap = image != null ? Display.create32bitDIB(image) : 0; } else { info.hbmpItem = image != null ? OS.HBMMENU_CALLBACK : 0; } } long /*int*/ hMenu = parent.handle; OS.SetMenuItemInfo(hMenu, id, false, info); parent.redraw(); }
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); Text text = new Text(shell, SWT.BORDER | SWT.V_SCROLL); text.setBounds(10, 10, 100, 100); for (int i = 0; i < 16; i++) { text.append("Line " + i + "\n"); } shell.open(); text.setSelection(30, 38); System.out.println(text.getCaretLocation()); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
/** * Returns the receiver's image data. This is the icon that is associated with the receiver in the * operating system. * * @return the image data for the program, may be null */ public ImageData getImageData() { NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); try { NSWorkspace workspace = NSWorkspace.sharedWorkspace(); NSString fullPath; if (this.fullPath != null) { fullPath = NSString.stringWith(this.fullPath); } else { fullPath = workspace.fullPathForApplication(NSString.stringWith(name)); } if (fullPath != null) { NSImage nsImage = workspace.iconForFile(fullPath); if (nsImage != null) { NSSize size = new NSSize(); size.width = size.height = 16; nsImage.setSize(size); nsImage.retain(); Image image = Image.cocoa_new(Display.getCurrent(), SWT.BITMAP, nsImage); ImageData imageData = image.getImageData(); image.dispose(); return imageData; } } return null; } finally { pool.release(); } }
/** * Returns the default menu item or null if none has been previously set. * * @return the default menu item. * </ul> * * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> */ public MenuItem getDefaultItem() { checkWidget(); int actionHandle = OS.QMenu_defaultAction(handle); Widget widget = Display.getWidget(actionHandle); if (widget != null) { if (MenuItem.class.isInstance(widget)) return (MenuItem) widget; } return null; }
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); RowLayout layout = new RowLayout(SWT.HORIZONTAL); layout.wrap = true; layout.fill = false; layout.justify = true; shell.setLayout(layout); Button b = new Button(shell, SWT.PUSH); b.setText("Button 1"); b = new Button(shell, SWT.PUSH); b.setText("Button 2"); b = new Button(shell, SWT.PUSH); b.setText("Button 3"); b = new Button(shell, SWT.PUSH); b.setText("Not shown"); b.setVisible(false); RowData data = new RowData(); data.exclude = true; b.setLayoutData(data); b = new Button(shell, SWT.PUSH); b.setText("Button 200 high"); data = new RowData(); data.height = 200; b.setLayoutData(data); b = new Button(shell, SWT.PUSH); b.setText("Button 200 wide"); data = new RowData(); data.width = 200; b.setLayoutData(data); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
void paint(PaintEvent event) { GC gc = event.gc; Display disp = getDisplay(); Rectangle rect = getClientArea(); gc.fillRectangle(rect); if (showBorder) { drawBevelRect( gc, rect.x, rect.y, rect.width - 1, rect.height - 1, disp.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW), disp.getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW)); } paintStripes(gc); }
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout(2, false)); final Text text = new Text(shell, SWT.SEARCH | SWT.ICON_CANCEL); Image image = null; if ((text.getStyle() & SWT.ICON_CANCEL) == 0) { image = display.getSystemImage(SWT.ICON_ERROR); ToolBar toolBar = new ToolBar(shell, SWT.FLAT); ToolItem item = new ToolItem(toolBar, SWT.PUSH); item.setImage(image); item.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { text.setText(""); System.out.println("Search cancelled"); } }); } text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); text.setText("Search text"); text.addSelectionListener( new SelectionAdapter() { @Override public void widgetDefaultSelected(SelectionEvent e) { if (e.detail == SWT.CANCEL) { System.out.println("Search cancelled"); } else { System.out.println("Searching for: " + text.getText() + "..."); } } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } if (image != null) image.dispose(); display.dispose(); }
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setText("Widget"); final Table table = new Table(shell, SWT.MULTI); table.setLinesVisible(true); table.setBounds(10, 10, 100, 100); for (int i = 0; i < 9; i++) { new TableItem(table, SWT.NONE).setText("item" + i); } Button button = new Button(shell, SWT.PUSH); button.setText("Capture"); button.pack(); button.setLocation(10, 140); button.addListener( SWT.Selection, event -> { Point tableSize = table.getSize(); GC gc = new GC(table); final Image image = new Image(display, tableSize.x, tableSize.y); gc.copyArea(image, 0, 0); gc.dispose(); Shell popup = new Shell(shell); popup.setText("Image"); popup.addListener(SWT.Close, e -> image.dispose()); Canvas canvas = new Canvas(popup, SWT.NONE); canvas.setBounds(10, 10, tableSize.x + 10, tableSize.y + 10); canvas.addPaintListener(e -> e.gc.drawImage(image, 0, 0)); popup.pack(); popup.open(); }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }