List of usage examples for org.eclipse.swt.widgets Button Button
public Button(Composite parent, int style)
From source file:org.eclipse.swt.snippets.Snippet144.java
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); shell.setText("Snippet 144"); shell.setLayout(new RowLayout(SWT.VERTICAL)); final Table table = new Table(shell, SWT.VIRTUAL | SWT.BORDER); table.addListener(SWT.SetData, event -> { TableItem item = (TableItem) event.item; int index = table.indexOf(item); item.setText("Item " + index); System.out.println(item.getText()); });//from www.jav a 2 s . c om table.setLayoutData(new RowData(200, 200)); Button button = new Button(shell, SWT.PUSH); button.setText("Add Items"); final Label label = new Label(shell, SWT.NONE); button.addListener(SWT.Selection, event -> { long t1 = System.currentTimeMillis(); table.setItemCount(COUNT); long t2 = System.currentTimeMillis(); label.setText("Items: " + COUNT + ", Time: " + (t2 - t1) + " (ms)"); shell.layout(); }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:GridLayoutDialog.java
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); Label labelUser;// w w w . j a v a2s. c o m Label labelFile; final Text textUser; final Text textFile; Button buttonBrowseFile; Button buttonUpload; GridLayout gridLayout = new GridLayout(3, false); shell.setLayout(gridLayout); labelUser = new Label(shell, SWT.NULL); GridData gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.grabExcessHorizontalSpace = true; textUser = new Text(shell, SWT.SINGLE | SWT.BORDER); textUser.setLayoutData(gridData); new Label(shell, SWT.NULL); // 2nd row. labelFile = new Label(shell, SWT.NULL); gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.grabExcessHorizontalSpace = true; textFile = new Text(shell, SWT.SINGLE | SWT.BORDER); textFile.setLayoutData(gridData); buttonBrowseFile = new Button(shell, SWT.PUSH); // last row. gridData = new GridData(); gridData.horizontalSpan = 3; gridData.horizontalAlignment = GridData.CENTER; buttonUpload = new Button(shell, SWT.PUSH); buttonUpload.setLayoutData(gridData); labelUser.setText("User name: "); labelFile.setText("Photo: "); buttonBrowseFile.setText("Browse"); buttonUpload.setText("Upload"); buttonBrowseFile.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { FileDialog dialog = new FileDialog(shell, SWT.OPEN); String file = dialog.open(); if (file != null) { textFile.setText(file); } } }); buttonUpload.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { System.out.println(textUser.getText()); System.out.println(textFile.getText()); shell.dispose(); } }); shell.pack(); shell.open(); textUser.forceFocus(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet358.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Snippet 358"); shell.setLayout(new GridLayout()); final Tree tree = new Tree(shell, SWT.NONE); tree.setLayoutData(new GridData(200, 200)); for (int i = 0; i < 9; i++) { TreeItem item = new TreeItem(tree, SWT.NONE); item.setText("root-level item " + i); for (int j = 0; j < 9; j++) { new TreeItem(item, SWT.NONE).setText("item " + i + "-" + j); }//from w w w .j av a 2 s. c om } Button button = new Button(shell, SWT.PUSH); button.setText("Print item visibilities"); button.addListener(SWT.Selection, event -> { Rectangle treeBounds = new Rectangle(0, 0, 0, 0); Point treeSize = tree.getSize(); treeBounds.width = treeSize.x; treeBounds.height = treeSize.y; TreeItem[] rootItems = tree.getItems(); for (int i = 0; i < rootItems.length; i++) { TreeItem rootItem = rootItems[i]; System.out.println(rootItem.getText() + " is at least partially visible? " + treeBounds.intersects(rootItem.getBounds())); TreeItem[] childItems = rootItem.getItems(); for (int j = 0; j < childItems.length; j++) { TreeItem childItem = childItems[j]; System.out.println(childItem.getText() + " is at least partially visible? " + treeBounds.intersects(childItem.getBounds())); } } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:MainClass.java
public static void main(String[] a) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setSize(250, 200);/*from w w w. j av a 2 s. c om*/ shell.setLayout(new FormLayout()); Composite controls = new Composite(shell, SWT.NONE); FormData data = new FormData(); data.top = new FormAttachment(0, 0); data.left = new FormAttachment(0, 0); data.right = new FormAttachment(100, 0); controls.setLayoutData(data); final Browser browser = new Browser(shell, SWT.NONE); data = new FormData(); data.top = new FormAttachment(controls); data.bottom = new FormAttachment(100, 0); data.left = new FormAttachment(0, 0); data.right = new FormAttachment(100, 0); browser.setLayoutData(data); controls.setLayout(new GridLayout(6, false)); Button button = new Button(controls, SWT.PUSH); button.setText("Back"); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { browser.back(); } }); button = new Button(controls, SWT.PUSH); button.setText("Forward"); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { browser.forward(); } }); button = new Button(controls, SWT.PUSH); button.setText("Refresh"); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { browser.refresh(); } }); button = new Button(controls, SWT.PUSH); button.setText("Stop"); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { browser.stop(); } }); final Text url = new Text(controls, SWT.BORDER); url.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); url.setFocus(); button = new Button(controls, SWT.PUSH); button.setText("Go"); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { browser.setUrl(url.getText()); } }); shell.setDefaultButton(button); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet169.java
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); shell.setText("Snippet 169"); shell.setLayout(new FillLayout()); Listener listener = e -> {/*w ww .ja v a2s. c o m*/ Control[] children = shell.getChildren(); for (int i = 0; i < children.length; i++) { Control child = children[i]; if (e.widget != child && child instanceof Button && (child.getStyle() & SWT.TOGGLE) != 0) { ((Button) child).setSelection(false); } } ((Button) e.widget).setSelection(true); }; for (int i = 0; i < 20; i++) { Button button = new Button(shell, SWT.TOGGLE); button.setText("B" + i); button.addListener(SWT.Selection, listener); if (i == 0) button.setSelection(true); } shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:Snippet116.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout()); Text text = new Text(shell, SWT.SINGLE | SWT.BORDER); text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); text.setText("Here is some text"); text.addSelectionListener(new SelectionAdapter() { public void widgetDefaultSelected(SelectionEvent e) { System.out.println("Text default selected (overrides default button)"); }/* w w w.j a v a2 s . co m*/ }); text.addTraverseListener(new TraverseListener() { public void keyTraversed(TraverseEvent e) { if (e.detail == SWT.TRAVERSE_RETURN) { e.doit = false; e.detail = SWT.TRAVERSE_NONE; } } }); Button button = new Button(shell, SWT.PUSH); button.setText("Ok"); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { System.out.println("Button selected"); } }); shell.setDefaultButton(button); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:ShellEscapeClose.java
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new FillLayout()); shell.addListener(SWT.Traverse, new Listener() { public void handleEvent(Event event) { switch (event.detail) { case SWT.TRAVERSE_ESCAPE: shell.close();/*from w ww .j av a2s.co m*/ event.detail = SWT.TRAVERSE_NONE; event.doit = false; break; } } }); Button button = new Button(shell, SWT.PUSH); button.setText("A Button (that doesn't process Escape)"); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:BusyCursorDisplay.java
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new GridLayout()); final Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL); text.setLayoutData(new GridData(GridData.FILL_BOTH)); final int[] nextId = new int[1]; Button b = new Button(shell, SWT.PUSH); b.setText("invoke long running job"); b.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { Runnable longJob = new Runnable() { boolean done = false; int id; public void run() { Thread thread = new Thread(new Runnable() { public void run() { id = nextId[0]++; display.syncExec(new Runnable() { public void run() { if (text.isDisposed()) return; text.append("\nStart long running task " + id); }// www. j a va2 s .com }); for (int i = 0; i < 100000; i++) { if (display.isDisposed()) return; System.out.println("do task that takes a long time in a separate thread " + id); } if (display.isDisposed()) return; display.syncExec(new Runnable() { public void run() { if (text.isDisposed()) return; text.append("\nCompleted long running task " + id); } }); done = true; display.wake(); } }); thread.start(); while (!done && !shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } }; BusyIndicator.showWhile(display, longJob); } }); shell.setSize(250, 150); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet103.java
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setText("Snippet 103"); shell.setBounds(10, 10, 200, 240);//from w w w. ja v a 2 s . c om Table table = new Table(shell, SWT.NONE); Rectangle clientArea = shell.getClientArea(); table.setBounds(clientArea.x + 10, clientArea.y + 10, 160, 160); final TableItem[] items = new TableItem[4]; for (int i = 0; i < 4; i++) { new TableColumn(table, SWT.NONE).setWidth(40); } for (int i = 0; i < 4; i++) { items[i] = new TableItem(table, SWT.NONE); populateItem(items[i]); } Button button = new Button(shell, SWT.PUSH); button.setText("Change"); button.pack(); button.setLocation(10, 180); button.addListener(SWT.Selection, event -> { for (int i = 0; i < 4; i++) { populateItem(items[i]); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:SashFormThreeChildren.java
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); SashForm form = new SashForm(shell, SWT.HORIZONTAL); form.setLayout(new FillLayout()); Composite child1 = new Composite(form, SWT.NONE); child1.setLayout(new FillLayout()); new Label(child1, SWT.NONE).setText("Label in pane 1"); Composite child2 = new Composite(form, SWT.NONE); child2.setLayout(new FillLayout()); new Button(child2, SWT.PUSH).setText("Button in pane2"); Composite child3 = new Composite(form, SWT.NONE); child3.setLayout(new FillLayout()); new Label(child3, SWT.PUSH).setText("Label in pane3"); form.setWeights(new int[] { 30, 40, 30 }); shell.open();//from w ww . j a v a 2 s.c om while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }