List of usage examples for org.eclipse.swt.widgets Button Button
public Button(Composite parent, int style)
From source file:org.eclipse.swt.snippets.Snippet363.java
public static void main(String[] args) { Display display = new Display(); errorIcon = display.getSystemImage(SWT.ICON_ERROR); Shell shell = new Shell(display); shell.setText("Snippet 363"); shell.setLayout(new GridLayout(2, false)); shell.setText("LiveRegion Test"); icon = new Label(shell, SWT.NONE); icon.setLayoutData(new GridData(32, 32)); liveLabel = new Text(shell, SWT.READ_ONLY); GC gc = new GC(liveLabel); Point pt = gc.textExtent(errorMessage); GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false); data.minimumWidth = (int) (pt.x + gc.getFontMetrics().getAverageCharacterWidth() * 2); gc.dispose();// ww w .jav a2 s .c o m liveLabel.setLayoutData(data); liveLabel.setText(""); liveLabel.getAccessible().addAccessibleAttributeListener(new AccessibleAttributeAdapter() { @Override public void getAttributes(AccessibleAttributeEvent e) { e.attributes = new String[] { "container-live", "polite", "live", "polite", "container-live-role", "status", }; } }); final Label textFieldLabel = new Label(shell, SWT.NONE); textFieldLabel.setText("Type a number:"); textFieldLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); final Text textField = new Text(shell, SWT.SINGLE | SWT.BORDER); textField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); final Button okButton = new Button(shell, SWT.PUSH); okButton.setText("OK"); okButton.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false, 2, 1)); okButton.setEnabled(false); textField.addModifyListener(e -> { boolean isNumber = false; String textValue = textField.getText(); try { Integer.parseInt(textValue); isNumber = true; setMessageText(false, "Thank-you"); } catch (NumberFormatException ex) { if (textValue.isEmpty()) { setMessageText(false, ""); } else { setMessageText(true, "Error: Number expected."); } } okButton.setEnabled(isNumber); }); textField.setFocus(); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet336.java
public static void main(String[] args) { display = new Display(); shell = new Shell(display); shell.setText("Snippet 336"); shell.setLayout(new GridLayout()); TabFolder folder = new TabFolder(shell, SWT.NONE); folder.setLayoutData(new GridData(GridData.FILL_BOTH)); //Progress tab TabItem item = new TabItem(folder, SWT.NONE); item.setText("Progress"); Composite composite = new Composite(folder, SWT.NONE); composite.setLayout(new GridLayout()); item.setControl(composite);/*from ww w . ja v a 2s . co m*/ Listener listener = event -> { Button button = (Button) event.widget; if (!button.getSelection()) return; TaskItem item1 = getTaskBarItem(); if (item1 != null) { int state = ((Integer) button.getData()).intValue(); item1.setProgressState(state); } }; Group group = new Group(composite, SWT.NONE); group.setText("State"); group.setLayout(new GridLayout()); group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); Button button; String[] stateLabels = { "SWT.DEFAULT", "SWT.INDETERMINATE", "SWT.NORMAL", "SWT.ERROR", "SWT.PAUSED" }; int[] states = { SWT.DEFAULT, SWT.INDETERMINATE, SWT.NORMAL, SWT.ERROR, SWT.PAUSED }; for (int i = 0; i < states.length; i++) { button = new Button(group, SWT.RADIO); button.setText(stateLabels[i]); button.setData(Integer.valueOf(states[i])); button.addListener(SWT.Selection, listener); if (i == 0) button.setSelection(true); } group = new Group(composite, SWT.NONE); group.setText("Value"); group.setLayout(new GridLayout(2, false)); group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); Label label = new Label(group, SWT.NONE); label.setText("Progress"); final Scale scale = new Scale(group, SWT.NONE); scale.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); scale.addListener(SWT.Selection, event -> { TaskItem item1 = getTaskBarItem(); if (item1 != null) item1.setProgress(scale.getSelection()); }); //Overlay text tab item = new TabItem(folder, SWT.NONE); item.setText("Text"); composite = new Composite(folder, SWT.NONE); composite.setLayout(new GridLayout()); item.setControl(composite); group = new Group(composite, SWT.NONE); group.setText("Enter a short text:"); group.setLayout(new GridLayout(2, false)); group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); final Text text = new Text(group, SWT.BORDER | SWT.SINGLE); GridData data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 2; text.setLayoutData(data); button = new Button(group, SWT.PUSH); button.setText("Set"); button.addListener(SWT.Selection, event -> { TaskItem item1 = getTaskBarItem(); if (item1 != null) item1.setOverlayText(text.getText()); }); button = new Button(group, SWT.PUSH); button.setText("Clear"); button.addListener(SWT.Selection, event -> { text.setText(""); TaskItem item1 = getTaskBarItem(); if (item1 != null) item1.setOverlayText(""); }); //Overlay image tab item = new TabItem(folder, SWT.NONE); item.setText("Image"); composite = new Composite(folder, SWT.NONE); composite.setLayout(new GridLayout()); item.setControl(composite); Listener listener3 = event -> { Button button1 = (Button) event.widget; if (!button1.getSelection()) return; TaskItem item1 = getTaskBarItem(); if (item1 != null) { String text1 = button1.getText(); Image image = null; if (!text1.equals("NONE")) image = new Image(display, Snippet336.class.getResourceAsStream(text1)); Image oldImage = item1.getOverlayImage(); item1.setOverlayImage(image); if (oldImage != null) oldImage.dispose(); } }; group = new Group(composite, SWT.NONE); group.setText("Images"); group.setLayout(new GridLayout()); group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); button = new Button(group, SWT.RADIO); button.setText("NONE"); button.addListener(SWT.Selection, listener3); button.setSelection(true); String[] images = { "eclipse.png", "pause.gif", "run.gif", "warning.gif" }; for (int i = 0; i < images.length; i++) { button = new Button(group, SWT.RADIO); button.setText(images[i]); button.addListener(SWT.Selection, listener3); } shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet375.java
public static void main(String[] args) throws Exception { final Display display = new Display(); final Shell shell = new Shell(display); shell.setText("Snippet 375"); shell.setLayout(new GridLayout(1, false)); final StringBuilder sb = new StringBuilder(); final Random random = new Random(2546); for (int i = 0; i < 200; i++) { sb.append("Very very long text about ").append(random.nextInt(2000)).append("\t"); if (i % 10 == 0) { sb.append("\n"); }// w ww. j a v a2 s. c o m } // H SCROLL final Label lbl1 = new Label(shell, SWT.NONE); lbl1.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false)); lbl1.setText("Horizontal Scroll"); final StyledText txt1 = new StyledText(shell, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL); txt1.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true)); txt1.setText(sb.toString()); txt1.setMouseNavigatorEnabled(true); // V_SCROLL final Label lbl2 = new Label(shell, SWT.NONE); lbl2.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false)); lbl2.setText("Vertical Scroll"); final StyledText txt2 = new StyledText(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL); txt2.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true)); txt2.setText(sb.toString()); txt2.setMouseNavigatorEnabled(true); // H SCROLL & V_SCROLL final Label lbl3 = new Label(shell, SWT.NONE); lbl3.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false)); lbl3.setText("Horizontal and Vertical Scroll"); final StyledText txt3 = new StyledText(shell, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); txt3.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true)); txt3.setText(sb.toString()); txt3.setMouseNavigatorEnabled(true); final Button enableDisableButton = new Button(shell, SWT.PUSH); enableDisableButton.setLayoutData(new GridData(GridData.END, GridData.FILL, true, false)); enableDisableButton.setText("Disable Mouse Navigation"); enableDisableButton.addListener(SWT.Selection, e -> { if (txt3.getMouseNavigatorEnabled()) { enableDisableButton.setText("Enable Mouse Navigation"); } else { enableDisableButton.setText("Disable Mouse Navigation"); } txt3.setMouseNavigatorEnabled(!txt3.getMouseNavigatorEnabled()); }); // Disabled Scroll at start final Label lbl4 = new Label(shell, SWT.NONE); lbl4.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false)); lbl4.setText("No scroll at start"); final StyledText txt4 = new StyledText(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); final GridData gd = new GridData(GridData.FILL, GridData.FILL, true, true); gd.minimumHeight = 100; txt4.setLayoutData(gd); txt4.setText("Disabled scroll"); txt4.setMouseNavigatorEnabled(true); // Disabled Scroll final Label lbl5 = new Label(shell, SWT.NONE); lbl5.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, false)); lbl5.setText("No scroll"); final StyledText txt5 = new StyledText(shell, SWT.MULTI | SWT.BORDER); final GridData gd5 = new GridData(GridData.FILL, GridData.FILL, true, true); gd5.minimumHeight = 100; txt5.setLayoutData(gd5); txt5.setText("No scroll"); txt5.setMouseNavigatorEnabled(true); shell.setSize(800, 600); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet193.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Snippet 193"); shell.setLayout(new RowLayout(SWT.HORIZONTAL)); final Tree tree = new Tree(shell, SWT.BORDER | SWT.CHECK); tree.setLayoutData(new RowData(-1, 300)); tree.setHeaderVisible(true);//from w w w.j av a 2s.c o m TreeColumn column = new TreeColumn(tree, SWT.LEFT); column.setText("Column 0"); column = new TreeColumn(tree, SWT.CENTER); column.setText("Column 1"); column = new TreeColumn(tree, SWT.LEFT); column.setText("Column 2"); column = new TreeColumn(tree, SWT.RIGHT); column.setText("Column 3"); column = new TreeColumn(tree, SWT.CENTER); column.setText("Column 4"); for (int i = 0; i < 5; i++) { TreeItem item = new TreeItem(tree, SWT.NONE); String[] text = new String[] { i + ":0", i + ":1", i + ":2", i + ":3", i + ":4" }; item.setText(text); for (int j = 0; j < 5; j++) { TreeItem subItem = new TreeItem(item, SWT.NONE); text = new String[] { i + "," + j + ":0", i + "," + j + ":1", i + "," + j + ":2", i + "," + j + ":3", i + "," + j + ":4" }; subItem.setText(text); for (int k = 0; k < 5; k++) { TreeItem subsubItem = new TreeItem(subItem, SWT.NONE); text = new String[] { i + "," + j + "," + k + ":0", i + "," + j + "," + k + ":1", i + "," + j + "," + k + ":2", i + "," + j + "," + k + ":3", i + "," + j + "," + k + ":4" }; subsubItem.setText(text); } } } Listener listener = e -> System.out.println("Move " + e.widget); TreeColumn[] columns = tree.getColumns(); for (int i = 0; i < columns.length; i++) { columns[i].setWidth(100); columns[i].setMoveable(true); columns[i].addListener(SWT.Move, listener); } Button b = new Button(shell, SWT.PUSH); b.setText("invert column order"); b.addListener(SWT.Selection, e -> { int[] order = tree.getColumnOrder(); for (int i = 0; i < order.length / 2; i++) { int temp = order[i]; order[i] = order[order.length - i - 1]; order[order.length - i - 1] = temp; } tree.setColumnOrder(order); }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet254.java
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setText("Snippet 254"); RowLayout layout = new RowLayout(SWT.VERTICAL); layout.fill = true;/* w w w . j a v a2s .co m*/ layout.wrap = false; shell.setLayout(layout); final Tree tree = new Tree(shell, SWT.NONE); for (int i = 0; i < 32; i++) { TreeItem item0 = new TreeItem(tree, SWT.NONE); item0.setText("Item " + i + " is quite long"); for (int j = 0; j < 3; j++) { TreeItem item1 = new TreeItem(item0, SWT.NONE); item1.setText("Item " + i + " " + j + " is quite long"); for (int k = 0; k < 3; k++) { TreeItem item2 = new TreeItem(item1, SWT.NONE); item2.setText("Item " + i + " " + j + " " + k + " is quite long"); for (int l = 0; l < 3; l++) { TreeItem item3 = new TreeItem(item2, SWT.NONE); item3.setText("Item " + i + " " + j + " " + k + " " + l + " is quite long"); } } } } tree.setLayoutData(new RowData(200, 200)); final Button button = new Button(shell, SWT.PUSH); button.setText("Visible Items []"); button.addListener(SWT.Selection, e -> { int visibleCount = 0; Rectangle rect = tree.getClientArea(); TreeItem item = tree.getTopItem(); while (item != null) { visibleCount++; Rectangle itemRect = item.getBounds(); if (itemRect.y + itemRect.height > rect.y + rect.height) { break; } item = nextItem(tree, item); } button.setText("Visible Items [" + visibleCount + "]"); }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet367.java
public static void main(String[] args) { final ImageFileNameProvider filenameProvider = zoom -> { switch (zoom) { case 100: return IMAGE_PATH_100; case 150: return IMAGE_PATH_150; case 200: return IMAGE_PATH_200; default:// w ww. j av a 2s .c om return null; } }; final ImageDataProvider imageDataProvider = zoom -> { switch (zoom) { case 100: return new ImageData(IMAGE_PATH_100); case 150: return new ImageData(IMAGE_PATH_150); case 200: return new ImageData(IMAGE_PATH_200); default: return null; } }; final Display display = new Display(); final Shell shell = new Shell(display); shell.setText("Snippet367"); shell.setLayout(new GridLayout(3, false)); Menu menuBar = new Menu(shell, SWT.BAR); shell.setMenuBar(menuBar); MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE); fileItem.setText("&File"); Menu fileMenu = new Menu(menuBar); fileItem.setMenu(fileMenu); MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH); exitItem.setText("&Exit"); exitItem.addListener(SWT.Selection, e -> shell.close()); new Label(shell, SWT.NONE).setText(IMAGE_200 + ":"); new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_200)); new Button(shell, SWT.PUSH).setImage(new Image(display, IMAGE_PATH_200)); new Label(shell, SWT.NONE).setText(IMAGE_150 + ":"); new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_150)); new Button(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_150)); new Label(shell, SWT.NONE).setText(IMAGE_100 + ":"); new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_100)); new Button(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_100)); createSeparator(shell); new Label(shell, SWT.NONE).setText("ImageFileNameProvider:"); new Label(shell, SWT.NONE).setImage(new Image(display, filenameProvider)); new Button(shell, SWT.NONE).setImage(new Image(display, filenameProvider)); new Label(shell, SWT.NONE).setText("ImageDataProvider:"); new Label(shell, SWT.NONE).setImage(new Image(display, imageDataProvider)); new Button(shell, SWT.NONE).setImage(new Image(display, imageDataProvider)); createSeparator(shell); new Label(shell, SWT.NONE).setText("1. Canvas\n(PaintListener)"); final Point size = new Point(550, 40); final Canvas canvas = new Canvas(shell, SWT.NONE); canvas.addPaintListener(e -> { Point size1 = canvas.getSize(); paintImage(e.gc, size1); }); GridData gridData = new GridData(size.x, size.y); gridData.horizontalSpan = 2; canvas.setLayoutData(gridData); createSeparator(shell); new Label(shell, SWT.NONE).setText("2. Painted image\n (default resolution)"); Image image = new Image(display, size.x, size.y); GC gc = new GC(image); try { paintImage(gc, size); } finally { gc.dispose(); } Label imageLabel = new Label(shell, SWT.NONE); imageLabel.setImage(image); imageLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1)); createSeparator(shell); new Label(shell, SWT.NONE).setText("3. Painted image\n(multi-res, unzoomed paint)"); imageLabel = new Label(shell, SWT.NONE); imageLabel.setImage(new Image(display, (ImageDataProvider) zoom -> { Image temp = new Image(display, size.x * zoom / 100, size.y * zoom / 100); GC gc1 = new GC(temp); try { paintImage(gc1, size); return temp.getImageData(); } finally { gc1.dispose(); temp.dispose(); } })); imageLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1)); createSeparator(shell); new Label(shell, SWT.NONE).setText("4. Painted image\n(multi-res, zoomed paint)"); imageLabel = new Label(shell, SWT.NONE); imageLabel.setImage(new Image(display, (ImageDataProvider) zoom -> { Image temp = new Image(display, size.x * zoom / 100, size.y * zoom / 100); GC gc1 = new GC(temp); try { paintImage2(gc1, new Point(size.x * zoom / 100, size.y * zoom / 100), zoom / 100); return temp.getImageData(); } finally { gc1.dispose(); temp.dispose(); } })); imageLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false, 2, 1)); createSeparator(shell); new Label(shell, SWT.NONE).setText("5. 50x50 box\n(Display#getDPI(): " + display.getDPI().x + ")"); Label box = new Label(shell, SWT.NONE); box.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW)); box.setLayoutData(new GridData(50, 50)); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:GridLayoutSimpleDmo.java
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); GridLayout gridLayout = new GridLayout(4, false); gridLayout.verticalSpacing = 8;/*from w w w . j av a2 s . com*/ shell.setLayout(gridLayout); Label label = new Label(shell, SWT.NULL); label.setText("Title: "); Text title = new Text(shell, SWT.SINGLE | SWT.BORDER); GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); gridData.horizontalSpan = 3; title.setLayoutData(gridData); label = new Label(shell, SWT.NULL); label.setText("Author(s): "); Text authors = new Text(shell, SWT.SINGLE | SWT.BORDER); gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL); gridData.horizontalSpan = 3; authors.setLayoutData(gridData); label = new Label(shell, SWT.NULL); label.setText("Cover: "); gridData = new GridData(); gridData.verticalSpan = 3; label.setLayoutData(gridData); CLabel cover = new CLabel(shell, SWT.NULL); gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.horizontalSpan = 1; gridData.verticalSpan = 3; gridData.heightHint = 100; gridData.widthHint = 100; cover.setLayoutData(gridData); label = new Label(shell, SWT.NULL); label.setText("Pages"); Text pages = new Text(shell, SWT.SINGLE | SWT.BORDER); pages.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); label = new Label(shell, SWT.NULL); label.setText("Publisher"); Text publisher = new Text(shell, SWT.SINGLE | SWT.BORDER); publisher.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); label = new Label(shell, SWT.NULL); label.setText("Rating"); Combo rating = new Combo(shell, SWT.READ_ONLY); rating.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); rating.add("5"); rating.add("4"); rating.add("3"); rating.add("2"); rating.add("1"); label = new Label(shell, SWT.NULL); label.setText("Abstract:"); Text bookAbstract = new Text(shell, SWT.WRAP | SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL); gridData.horizontalSpan = 3; gridData.grabExcessVerticalSpace = true; bookAbstract.setLayoutData(gridData); Button enter = new Button(shell, SWT.PUSH); enter.setText("Enter"); gridData = new GridData(); gridData.horizontalSpan = 4; gridData.horizontalAlignment = GridData.END; enter.setLayoutData(gridData); title.setText("Professional Java Interfaces with SWT/JFace"); authors.setText("Jack Li Guojie"); pages.setText("500pp"); publisher.setText("John Wiley & Sons"); cover.setBackground(new Image(display, "yourFile.gif")); bookAbstract.setText("SWT/JFace. "); shell.setSize(450, 400); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet134.java
public static void main(String[] args) { final Display display = new Display(); //Shell must be created with style SWT.NO_TRIM final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP); shell.setText("Snippet 134"); shell.setBackground(display.getSystemColor(SWT.COLOR_RED)); //define a region that looks like a key hole Region region = new Region(); region.add(circle(67, 67, 67));/*from w ww . ja v a2s . c o m*/ region.subtract(circle(20, 67, 50)); region.subtract(new int[] { 67, 50, 55, 105, 79, 105 }); //define the shape of the shell using setRegion shell.setRegion(region); Rectangle size = region.getBounds(); shell.setSize(size.width, size.height); //add ability to move shell around Listener l = new Listener() { /** The x/y of the MouseDown, relative to top-left of the shell. */ Point origin; @Override public void handleEvent(Event e) { switch (e.type) { case SWT.MouseDown: Point point = shell.toDisplay(e.x, e.y); Point loc = shell.getLocation(); origin = new Point(point.x - loc.x, point.y - loc.y); break; case SWT.MouseUp: origin = null; break; case SWT.MouseMove: if (origin != null) { Point p = display.map(shell, null, e.x, e.y); shell.setLocation(p.x - origin.x, p.y - origin.y); } break; } } }; shell.addListener(SWT.MouseDown, l); shell.addListener(SWT.MouseUp, l); shell.addListener(SWT.MouseMove, l); //add ability to close shell Button b = new Button(shell, SWT.PUSH); b.setBackground(shell.getBackground()); b.setText("close"); b.pack(); b.setLocation(10, 68); b.addListener(SWT.Selection, e -> shell.close()); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } region.dispose(); display.dispose(); }
From source file:org.eclipse.swt.examples.accessibility.AccessibleTableExample.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout()); shell.setText("Accessible Table Example"); Group group = new Group(shell, SWT.NONE); group.setText("Tables With Accessible Cell Children"); group.setLayout(new GridLayout()); new Label(group, SWT.NONE).setText("CTable with column headers"); table1 = new CTable(group, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER); table1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); table1.setHeaderVisible(true);//ww w .j av a 2 s.c om table1.setLinesVisible(true); for (int col = 0; col < 3; col++) { CTableColumn column = new CTableColumn(table1, SWT.NONE); column.setText("Col " + col); column.setWidth(50); } for (int row = 0; row < 4; row++) { CTableItem item = new CTableItem(table1, SWT.NONE); item.setText(new String[] { "C0R" + row, "C1R" + row, "C2R" + row }); } Composite btnGroup = new Composite(group, SWT.NONE); btnGroup.setLayout(new FillLayout(SWT.VERTICAL)); Button btn = new Button(btnGroup, SWT.PUSH); btn.setText("Add rows"); btn.addSelectionListener(widgetSelectedAdapter(e -> { int currSize = table1.getItemCount(); int colCount = table1.getColumnCount(); CTableItem item = new CTableItem(table1, SWT.NONE); String[] cells = new String[colCount]; for (int i = 0; i < colCount; i++) { cells[i] = "C" + i + "R" + currSize; } item.setText(cells); })); btn = new Button(btnGroup, SWT.PUSH); btn.setText("Remove rows"); btn.addSelectionListener(widgetSelectedAdapter(e -> { int currSize = table1.getItemCount(); if (currSize > 0) { table1.remove(currSize - 1); } })); btn = new Button(btnGroup, SWT.PUSH); btn.setText("Remove selected rows"); btn.addSelectionListener(widgetSelectedAdapter(e -> { CTableItem[] selectedItems = table1.getSelection(); for (CTableItem selectedItem : selectedItems) { selectedItem.dispose(); } })); btn = new Button(btnGroup, SWT.PUSH); btn.setText("Add column"); btn.addSelectionListener(widgetSelectedAdapter(e -> { int currSize = table1.getColumnCount(); CTableColumn item = new CTableColumn(table1, SWT.NONE); item.setText("Col " + currSize); item.setWidth(50); })); btn = new Button(btnGroup, SWT.PUSH); btn.setText("Remove last column"); btn.addSelectionListener(widgetSelectedAdapter(e -> { int colCount = table1.getColumnCount(); if (colCount > 0) { CTableColumn column = table1.getColumn(colCount - 1); column.dispose(); } })); new Label(group, SWT.NONE).setText("CTable used as a list"); CTable table2 = new CTable(group, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER); table2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); table2.setLinesVisible(true); for (String element : itemText) { CTableItem item = new CTableItem(table2, SWT.NONE); item.setText(element); } shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet338.java
public static void main(String[] args) { Display display = new Display(); Shell parentShell = new Shell(display); parentShell.setText("Snippet 338"); parentShell.setBounds(10, 10, 100, 100); parentShell.open();/*from w w w. ja v a 2 s .c om*/ Shell childShell = new Shell(parentShell); childShell.setLayout(new GridLayout()); TabFolder folder = new TabFolder(childShell, SWT.NONE); folder.setLayout(new FillLayout()); TabItem tab1 = new TabItem(folder, SWT.NONE); tab1.setText("Tab &1"); new TabItem(folder, SWT.NONE).setText("Tab &2"); Composite composite = new Composite(folder, SWT.NONE); composite.setLayout(new GridLayout()); tab1.setControl(composite); Text text1 = new Text(composite, SWT.SINGLE); /* canvas represents a custom control */ final Canvas canvas = new Canvas(composite, SWT.BORDER); canvas.setLayoutData(new GridData(300, 200)); canvas.addListener(SWT.Paint, event -> { if (canvas.isFocusControl()) { event.gc.drawText( "focus is here, custom traverse keys:\n\tN: Tab next\n\tP: Tab previous\n\tR: Return\n\tE: Esc\n\tT: Tab Folder next page", 0, 0); } else { event.gc.drawString("focus is not in this control", 0, 0); } }); canvas.addListener(SWT.KeyDown, event -> { int traversal = SWT.NONE; switch (event.keyCode) { case 'n': traversal = SWT.TRAVERSE_TAB_NEXT; break; case 'p': traversal = SWT.TRAVERSE_TAB_PREVIOUS; break; case 'r': traversal = SWT.TRAVERSE_RETURN; break; case 'e': traversal = SWT.TRAVERSE_ESCAPE; break; case 't': traversal = SWT.TRAVERSE_PAGE_NEXT; break; } if (traversal != SWT.NONE) { event.doit = true; /* this will be the Traverse event's initial doit value */ canvas.traverse(traversal, event); } }); canvas.addFocusListener(focusLostAdapter(e -> canvas.redraw())); canvas.addFocusListener(focusGainedAdapter(e -> canvas.redraw())); Text text2 = new Text(composite, SWT.SINGLE); Button button = new Button(childShell, SWT.PUSH); button.setText("Default &Button"); button.addListener(SWT.Selection, event -> System.out.println("Default button selected")); childShell.setDefaultButton(button); Listener printTraverseListener = event -> { StringBuilder buffer = new StringBuilder("Traverse "); buffer.append(event.widget); buffer.append(" type="); switch (event.detail) { case SWT.TRAVERSE_ARROW_NEXT: buffer.append("TRAVERSE_ARROW_NEXT"); break; case SWT.TRAVERSE_ARROW_PREVIOUS: buffer.append("TRAVERSE_ARROW_NEXT"); break; case SWT.TRAVERSE_ESCAPE: buffer.append("TRAVERSE_ESCAPE"); break; case SWT.TRAVERSE_MNEMONIC: buffer.append("TRAVERSE_MNEMONIC"); break; case SWT.TRAVERSE_PAGE_NEXT: buffer.append("TRAVERSE_PAGE_NEXT"); break; case SWT.TRAVERSE_PAGE_PREVIOUS: buffer.append("TRAVERSE_PAGE_PREVIOUS"); break; case SWT.TRAVERSE_RETURN: buffer.append("TRAVERSE_RETURN"); break; case SWT.TRAVERSE_TAB_NEXT: buffer.append("TRAVERSE_TAB_NEXT"); break; case SWT.TRAVERSE_TAB_PREVIOUS: buffer.append("TRAVERSE_TAB_PREVIOUS"); break; } buffer.append(" doit=" + event.doit); buffer.append(" keycode=" + event.keyCode); buffer.append(" char=" + event.character); buffer.append(" stateMask=" + event.stateMask); System.out.println(buffer.toString()); }; childShell.addListener(SWT.Traverse, printTraverseListener); folder.addListener(SWT.Traverse, printTraverseListener); composite.addListener(SWT.Traverse, printTraverseListener); canvas.addListener(SWT.Traverse, printTraverseListener); button.addListener(SWT.Traverse, printTraverseListener); text1.addListener(SWT.Traverse, printTraverseListener); text2.addListener(SWT.Traverse, printTraverseListener); childShell.pack(); childShell.open(); text1.setFocus(); while (!parentShell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }