List of usage examples for org.eclipse.swt.widgets Composite Composite
public Composite(Composite parent, int style)
From source file:org.eclipse.swt.snippets.Snippet209.java
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); Composite comp = new Composite(shell, SWT.NONE); comp.setLayout(new FillLayout()); GLData data = new GLData(); data.doubleBuffer = true;//from w w w . j a va 2 s .com final GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data); canvas.setCurrent(); final GLContext context = GLDrawableFactory.getFactory(GLProfile.getGL2GL3()).createExternalGLContext(); ; canvas.addListener(SWT.Resize, new Listener() { @Override public void handleEvent(Event event) { Rectangle bounds = canvas.getBounds(); float fAspect = (float) bounds.width / (float) bounds.height; canvas.setCurrent(); context.makeCurrent(); GL2 gl = (GL2) context.getGL(); gl.glViewport(0, 0, bounds.width, bounds.height); gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity(); GLU glu = new GLU(); glu.gluPerspective(45.0f, fAspect, 0.5f, 400.0f); gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glLoadIdentity(); context.release(); } }); context.makeCurrent(); GL2 gl = (GL2) context.getGL(); gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); gl.glColor3f(1.0f, 0.0f, 0.0f); gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); gl.glClearDepth(1.0); gl.glLineWidth(2); gl.glEnable(GL.GL_DEPTH_TEST); context.release(); shell.setText("SWT/JOGL Example"); shell.setSize(640, 480); shell.open(); display.asyncExec(new Runnable() { int rot = 0; @Override public void run() { if (!canvas.isDisposed()) { canvas.setCurrent(); context.makeCurrent(); GL2 gl = (GL2) context.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glClearColor(.3f, .5f, .8f, 1.0f); gl.glLoadIdentity(); gl.glTranslatef(0.0f, 0.0f, -10.0f); float frot = rot; gl.glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f); gl.glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f); rot++; gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2.GL_LINE); gl.glColor3f(0.9f, 0.9f, 0.9f); drawTorus(gl, 1, 1.9f + ((float) Math.sin((0.004f * frot))), 15, 15); canvas.swapBuffers(); context.release(); display.asyncExec(this); } } }); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet195.java
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); Composite comp = new Composite(shell, SWT.NONE); comp.setLayout(new FillLayout()); GLData data = new GLData(); data.doubleBuffer = true;/*from w w w . ja v a2 s .c om*/ final GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data); canvas.setCurrent(); try { GLContext.useContext(canvas); } catch (LWJGLException e) { e.printStackTrace(); } canvas.addListener(SWT.Resize, new Listener() { public void handleEvent(Event event) { Rectangle bounds = canvas.getBounds(); float fAspect = (float) bounds.width / (float) bounds.height; canvas.setCurrent(); try { GLContext.useContext(canvas); } catch (LWJGLException e) { e.printStackTrace(); } GL11.glViewport(0, 0, bounds.width, bounds.height); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GLU.gluPerspective(45.0f, fAspect, 0.5f, 400.0f); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glLoadIdentity(); } }); GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); GL11.glColor3f(1.0f, 0.0f, 0.0f); GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); GL11.glClearDepth(1.0); GL11.glLineWidth(2); GL11.glEnable(GL11.GL_DEPTH_TEST); shell.setText("SWT/LWJGL Example"); shell.setSize(640, 480); shell.open(); display.asyncExec(new Runnable() { int rot = 0; public void run() { if (!canvas.isDisposed()) { canvas.setCurrent(); try { GLContext.useContext(canvas); } catch (LWJGLException e) { e.printStackTrace(); } GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); GL11.glClearColor(.3f, .5f, .8f, 1.0f); GL11.glLoadIdentity(); GL11.glTranslatef(0.0f, 0.0f, -10.0f); float frot = rot; GL11.glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f); GL11.glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f); rot++; GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE); GL11.glColor3f(0.9f, 0.9f, 0.9f); drawTorus(1, 1.9f + ((float) Math.sin((0.004f * frot))), 15, 15); canvas.swapBuffers(); display.asyncExec(this); } } }); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet209.java
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); Composite comp = new Composite(shell, SWT.NONE); comp.setLayout(new FillLayout()); GLData data = new GLData(); data.doubleBuffer = true;//from w ww . jav a 2 s . c o m final GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data); canvas.setCurrent(); final GLContext context = GLDrawableFactory.getFactory().createExternalGLContext(); canvas.addListener(SWT.Resize, new Listener() { public void handleEvent(Event event) { Rectangle bounds = canvas.getBounds(); float fAspect = (float) bounds.width / (float) bounds.height; canvas.setCurrent(); context.makeCurrent(); GL gl = context.getGL(); gl.glViewport(0, 0, bounds.width, bounds.height); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); GLU glu = new GLU(); glu.gluPerspective(45.0f, fAspect, 0.5f, 400.0f); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); context.release(); } }); context.makeCurrent(); GL gl = context.getGL(); gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); gl.glColor3f(1.0f, 0.0f, 0.0f); gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); gl.glClearDepth(1.0); gl.glLineWidth(2); gl.glEnable(GL.GL_DEPTH_TEST); context.release(); shell.setText("SWT/JOGL Example"); shell.setSize(640, 480); shell.open(); display.asyncExec(new Runnable() { int rot = 0; public void run() { if (!canvas.isDisposed()) { canvas.setCurrent(); context.makeCurrent(); GL gl = context.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glClearColor(.3f, .5f, .8f, 1.0f); gl.glLoadIdentity(); gl.glTranslatef(0.0f, 0.0f, -10.0f); float frot = rot; gl.glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f); gl.glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f); rot++; gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE); gl.glColor3f(0.9f, 0.9f, 0.9f); drawTorus(gl, 1, 1.9f + ((float) Math.sin((0.004f * frot))), 15, 15); canvas.swapBuffers(); context.release(); display.asyncExec(this); } } }); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:ViewFormCreate.java
public static void main(String[] args) { Shell shell = new Shell(display); shell.setText("Look"); shell.setLayout(new GridLayout(1, false)); Button button = new Button(shell, SWT.PUSH); button.setText("New Document"); final Composite composite = new Composite(shell, SWT.NONE); composite.setLayoutData(new GridData(GridData.FILL_BOTH)); composite.setLayout(new FillLayout()); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { createViewFormHelper(composite, "Document " + (++count)); composite.layout();/*from w w w . ja v a 2 s . c o m*/ } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } 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);/* w ww. ja v a2 s . co m*/ 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.Snippet361.java
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setText("Snippet 361"); shell.setText("Translate and Rotate an AWT Image in an SWT GUI"); shell.setLayout(new GridLayout(8, false)); Button fileButton = new Button(shell, SWT.PUSH); fileButton.setText("&Open Image File"); fileButton.addSelectionListener(widgetSelectedAdapter(e -> { String filename = new FileDialog(shell).open(); if (filename != null) { image = Toolkit.getDefaultToolkit().getImage(filename); canvas.repaint();//from ww w . j a v a 2 s . c o m } })); new Label(shell, SWT.NONE).setText("Translate &X by:"); final Combo translateXCombo = new Combo(shell, SWT.NONE); translateXCombo.setItems("0", "image width", "image height", "100", "200"); translateXCombo.select(0); translateXCombo.addModifyListener(e -> { translateX = numericValue(translateXCombo); canvas.repaint(); }); new Label(shell, SWT.NONE).setText("Translate &Y by:"); final Combo translateYCombo = new Combo(shell, SWT.NONE); translateYCombo.setItems("0", "image width", "image height", "100", "200"); translateYCombo.select(0); translateYCombo.addModifyListener(e -> { translateY = numericValue(translateYCombo); canvas.repaint(); }); new Label(shell, SWT.NONE).setText("&Rotate by:"); final Combo rotateCombo = new Combo(shell, SWT.NONE); rotateCombo.setItems("0", "Pi", "Pi/2", "Pi/4", "Pi/8"); rotateCombo.select(0); rotateCombo.addModifyListener(e -> { rotate = numericValue(rotateCombo); canvas.repaint(); }); Button printButton = new Button(shell, SWT.PUSH); printButton.setText("&Print Image"); printButton.addSelectionListener(widgetSelectedAdapter(e -> { performPrintAction(display, shell); })); composite = new Composite(shell, SWT.EMBEDDED | SWT.BORDER); GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 8, 1); data.widthHint = 640; data.heightHint = 480; composite.setLayoutData(data); Frame frame = SWT_AWT.new_Frame(composite); canvas = new Canvas() { @Override public void paint(Graphics g) { if (image != null) { g.setColor(Color.WHITE); g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight()); /* Use Java2D here to modify the image as desired. */ Graphics2D g2d = (Graphics2D) g; AffineTransform t = new AffineTransform(); t.translate(translateX, translateY); t.rotate(rotate); g2d.setTransform(t); /*------------*/ g.drawImage(image, 0, 0, this); } } }; frame.add(canvas); composite.getAccessible().addAccessibleListener(new AccessibleAdapter() { @Override public void getName(AccessibleEvent e) { e.result = "Image drawn in AWT Canvas"; } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet135.java
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setText("SWT and Swing/AWT Example"); Listener exitListener = e -> {/*from www .java 2 s . c om*/ MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION); dialog.setText("Question"); dialog.setMessage("Exit?"); if (e.type == SWT.Close) e.doit = false; if (dialog.open() != SWT.OK) return; shell.dispose(); }; Listener aboutListener = e -> { final Shell s = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); s.setText("About"); GridLayout layout = new GridLayout(1, false); layout.verticalSpacing = 20; layout.marginHeight = layout.marginWidth = 10; s.setLayout(layout); Label label = new Label(s, SWT.NONE); label.setText("SWT and AWT Example."); Button button = new Button(s, SWT.PUSH); button.setText("OK"); GridData data = new GridData(); data.horizontalAlignment = GridData.CENTER; button.setLayoutData(data); button.addListener(SWT.Selection, event -> s.dispose()); s.pack(); Rectangle parentBounds = shell.getBounds(); Rectangle bounds = s.getBounds(); int x = parentBounds.x + (parentBounds.width - bounds.width) / 2; int y = parentBounds.y + (parentBounds.height - bounds.height) / 2; s.setLocation(x, y); s.open(); while (!s.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } }; shell.addListener(SWT.Close, exitListener); Menu mb = new Menu(shell, SWT.BAR); MenuItem fileItem = new MenuItem(mb, SWT.CASCADE); fileItem.setText("&File"); Menu fileMenu = new Menu(shell, SWT.DROP_DOWN); fileItem.setMenu(fileMenu); MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH); exitItem.setText("&Exit\tCtrl+X"); exitItem.setAccelerator(SWT.CONTROL + 'X'); exitItem.addListener(SWT.Selection, exitListener); MenuItem aboutItem = new MenuItem(fileMenu, SWT.PUSH); aboutItem.setText("&About\tCtrl+A"); aboutItem.setAccelerator(SWT.CONTROL + 'A'); aboutItem.addListener(SWT.Selection, aboutListener); shell.setMenuBar(mb); RGB color = shell.getBackground().getRGB(); Label separator1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); Label locationLb = new Label(shell, SWT.NONE); locationLb.setText("Location:"); Composite locationComp = new Composite(shell, SWT.EMBEDDED); ToolBar toolBar = new ToolBar(shell, SWT.FLAT); ToolItem exitToolItem = new ToolItem(toolBar, SWT.PUSH); exitToolItem.setText("&Exit"); exitToolItem.addListener(SWT.Selection, exitListener); ToolItem aboutToolItem = new ToolItem(toolBar, SWT.PUSH); aboutToolItem.setText("&About"); aboutToolItem.addListener(SWT.Selection, aboutListener); Label separator2 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); final Composite comp = new Composite(shell, SWT.NONE); final Tree fileTree = new Tree(comp, SWT.SINGLE | SWT.BORDER); Sash sash = new Sash(comp, SWT.VERTICAL); Composite tableComp = new Composite(comp, SWT.EMBEDDED); Label separator3 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); Composite statusComp = new Composite(shell, SWT.EMBEDDED); java.awt.Frame locationFrame = SWT_AWT.new_Frame(locationComp); final java.awt.TextField locationText = new java.awt.TextField(); locationFrame.add(locationText); java.awt.Frame fileTableFrame = SWT_AWT.new_Frame(tableComp); java.awt.Panel panel = new java.awt.Panel(new java.awt.BorderLayout()); fileTableFrame.add(panel); final JTable fileTable = new JTable(new FileTableModel(null)); fileTable.setDoubleBuffered(true); fileTable.setShowGrid(false); fileTable.createDefaultColumnsFromModel(); JScrollPane scrollPane = new JScrollPane(fileTable); panel.add(scrollPane); java.awt.Frame statusFrame = SWT_AWT.new_Frame(statusComp); statusFrame.setBackground(new java.awt.Color(color.red, color.green, color.blue)); final java.awt.Label statusLabel = new java.awt.Label(); statusFrame.add(statusLabel); statusLabel.setText("Select a file"); sash.addListener(SWT.Selection, e -> { if (e.detail == SWT.DRAG) return; GridData data = (GridData) fileTree.getLayoutData(); Rectangle trim = fileTree.computeTrim(0, 0, 0, 0); data.widthHint = e.x - trim.width; comp.layout(); }); File[] roots = File.listRoots(); for (int i = 0; i < roots.length; i++) { File file = roots[i]; TreeItem treeItem = new TreeItem(fileTree, SWT.NONE); treeItem.setText(file.getAbsolutePath()); treeItem.setData(file); new TreeItem(treeItem, SWT.NONE); } fileTree.addListener(SWT.Expand, e -> { TreeItem item = (TreeItem) e.item; if (item == null) return; if (item.getItemCount() == 1) { TreeItem firstItem = item.getItems()[0]; if (firstItem.getData() != null) return; firstItem.dispose(); } else { return; } File root = (File) item.getData(); File[] files = root.listFiles(); if (files == null) return; for (int i = 0; i < files.length; i++) { File file = files[i]; if (file.isDirectory()) { TreeItem treeItem = new TreeItem(item, SWT.NONE); treeItem.setText(file.getName()); treeItem.setData(file); new TreeItem(treeItem, SWT.NONE); } } }); fileTree.addListener(SWT.Selection, e -> { TreeItem item = (TreeItem) e.item; if (item == null) return; final File root = (File) item.getData(); EventQueue.invokeLater(() -> { statusLabel.setText(root.getAbsolutePath()); locationText.setText(root.getAbsolutePath()); fileTable.setModel(new FileTableModel(root.listFiles())); }); }); GridLayout layout = new GridLayout(4, false); layout.marginWidth = layout.marginHeight = 0; layout.horizontalSpacing = layout.verticalSpacing = 1; shell.setLayout(layout); GridData data; data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 4; separator1.setLayoutData(data); data = new GridData(); data.horizontalSpan = 1; data.horizontalIndent = 10; locationLb.setLayoutData(data); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 2; data.heightHint = locationText.getPreferredSize().height; locationComp.setLayoutData(data); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 1; toolBar.setLayoutData(data); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 4; separator2.setLayoutData(data); data = new GridData(GridData.FILL_BOTH); data.horizontalSpan = 4; comp.setLayoutData(data); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 4; separator3.setLayoutData(data); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 4; data.heightHint = statusLabel.getPreferredSize().height; statusComp.setLayoutData(data); layout = new GridLayout(3, false); layout.marginWidth = layout.marginHeight = 0; layout.horizontalSpacing = layout.verticalSpacing = 1; comp.setLayout(layout); data = new GridData(GridData.FILL_VERTICAL); data.widthHint = 200; fileTree.setLayoutData(data); data = new GridData(GridData.FILL_VERTICAL); sash.setLayoutData(data); data = new GridData(GridData.FILL_BOTH); tableComp.setLayoutData(data); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet373.java
@SuppressWarnings("restriction") public static void main(String[] args) { System.setProperty("swt.autoScale", "quarter"); Display display = new Display(); final Image eclipse = new Image(display, filenameProvider); final Image eclipseToolBar1 = new Image(display, filenameProvider); final Image eclipseToolBar2 = new Image(display, filenameProvider); final Image eclipseTableHeader = new Image(display, filenameProvider); final Image eclipseTableItem = new Image(display, filenameProvider); final Image eclipseTree1 = new Image(display, filenameProvider); final Image eclipseTree2 = new Image(display, filenameProvider); final Image eclipseCTab1 = new Image(display, filenameProvider); final Image eclipseCTab2 = new Image(display, filenameProvider); Shell shell = new Shell(display); shell.setText("Snippet 373"); shell.setImage(eclipse);/* w ww. j av a2s. c o m*/ shell.setText("DynamicDPI @ " + DPIUtil.getDeviceZoom()); shell.setLayout(new RowLayout(SWT.VERTICAL)); shell.setLocation(100, 100); shell.setSize(500, 600); shell.addListener(SWT.ZoomChanged, new Listener() { @Override public void handleEvent(Event e) { if (display.getPrimaryMonitor().equals(shell.getMonitor())) { MessageBox box = new MessageBox(shell, SWT.PRIMARY_MODAL | SWT.OK | SWT.CANCEL); box.setText(shell.getText()); box.setMessage("DPI changed, do you want to exit & restart ?"); e.doit = (box.open() == SWT.OK); if (e.doit) { shell.close(); System.out.println("Program exit."); } } } }); // Menu Menu bar = new Menu(shell, SWT.BAR); shell.setMenuBar(bar); MenuItem fileItem = new MenuItem(bar, SWT.CASCADE); fileItem.setText("&File"); fileItem.setImage(eclipse); Menu submenu = new Menu(shell, SWT.DROP_DOWN); fileItem.setMenu(submenu); MenuItem subItem = new MenuItem(submenu, SWT.PUSH); subItem.addListener(SWT.Selection, e -> System.out.println("Select All")); subItem.setText("Select &All\tCtrl+A"); subItem.setAccelerator(SWT.MOD1 + 'A'); subItem.setImage(eclipse); // CTabFolder CTabFolder folder = new CTabFolder(shell, SWT.BORDER); for (int i = 0; i < 2; i++) { CTabItem cTabItem = new CTabItem(folder, i % 2 == 0 ? SWT.CLOSE : SWT.NONE); cTabItem.setText("Item " + i); Text textMsg = new Text(folder, SWT.MULTI); textMsg.setText("Content for Item " + i); cTabItem.setControl(textMsg); cTabItem.setImage((i % 2 == 1) ? eclipseCTab1 : eclipseCTab2); } // PerMonitorV2 setting // Label label = new Label (shell, SWT.BORDER); // label.setText("PerMonitorV2 value before:after:Error"); // Text text = new Text(shell, SWT.BORDER); // text.setText(DPIUtil.BEFORE + ":" + DPIUtil.AFTER + ":" + DPIUtil.RESULT); // Composite for Label, Button, Tool-bar Composite composite = new Composite(shell, SWT.BORDER); composite.setLayout(new RowLayout(SWT.HORIZONTAL)); // Label with Image Label label1 = new Label(composite, SWT.BORDER); label1.setImage(eclipse); // Label with text only Label label2 = new Label(composite, SWT.BORDER); label2.setText("No Image"); // Button with text + Old Image Constructor Button oldButton1 = new Button(composite, SWT.PUSH); oldButton1.setText("Old Img"); oldButton1.setImage(new Image(display, IMAGE_PATH_100)); // Button with Old Image Constructor // Button oldButton2 = new Button(composite, SWT.PUSH); // oldButton2.setImage(new Image(display, filenameProvider.getImagePath(100))); // Button with Image Button createDialog = new Button(composite, SWT.PUSH); createDialog.setText("Child Dialog"); createDialog.setImage(eclipse); createDialog.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent event) { final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.RESIZE); dialog.setText("Child Dialog"); RowLayout rowLayout = new RowLayout(SWT.VERTICAL); dialog.setLayout(rowLayout); Label label = new Label(dialog, SWT.BORDER); label.setImage(eclipse); Point location = shell.getLocation(); dialog.setLocation(location.x + 250, location.y + 50); dialog.pack(); dialog.open(); } }); // Toolbar with Image ToolBar toolBar = new ToolBar(composite, SWT.FLAT | SWT.BORDER); Rectangle clientArea = shell.getClientArea(); toolBar.setLocation(clientArea.x, clientArea.y); for (int i = 0; i < 2; i++) { int style = i % 2 == 1 ? SWT.DROP_DOWN : SWT.PUSH; ToolItem toolItem = new ToolItem(toolBar, style); toolItem.setImage((i % 2 == 0) ? eclipseToolBar1 : eclipseToolBar2); toolItem.setEnabled(i % 2 == 0); } toolBar.pack(); Button button = new Button(shell, SWT.PUSH); button.setText("Refresh-Current Monitor : Zoom"); Text text1 = new Text(shell, SWT.BORDER); Monitor monitor = button.getMonitor(); text1.setText("" + monitor.getZoom()); button.addMouseListener(new MouseAdapter() { @Override public void mouseDown(MouseEvent e) { Monitor monitor = button.getMonitor(); text1.setText("" + monitor.getZoom()); } }); Button button2 = new Button(shell, SWT.PUSH); button2.setText("Refresh-Both Monitors : Zoom"); Text text2 = new Text(shell, SWT.BORDER); Monitor[] monitors = display.getMonitors(); StringBuilder text2String = new StringBuilder(); for (int i = 0; i < monitors.length; i++) { text2String.append(monitors[i].getZoom() + (i < (monitors.length - 1) ? " - " : "")); } text2.setText(text2String.toString()); button2.addMouseListener(new MouseAdapter() { @Override public void mouseDown(MouseEvent e) { Monitor[] monitors = display.getMonitors(); StringBuilder text2String = new StringBuilder(); for (int i = 0; i < monitors.length; i++) { text2String.append(monitors[i].getZoom() + (i < (monitors.length - 1) ? " - " : "")); } text2.setText(text2String.toString()); } }); // Table Table table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION); table.setLinesVisible(true); table.setHeaderVisible(true); String titles[] = { "Title 1" }; for (int i = 0; i < titles.length; i++) { TableColumn column = new TableColumn(table, SWT.NONE); column.setText(titles[i]); column.setImage(eclipseTableHeader); } for (int i = 0; i < 1; i++) { TableItem item = new TableItem(table, SWT.NONE); item.setText(0, "Data " + i); item.setImage(0, eclipseTableItem); } for (int i = 0; i < titles.length; i++) { table.getColumn(i).pack(); } // Tree final Tree tree = new Tree(shell, SWT.BORDER); for (int i = 0; i < 1; i++) { TreeItem iItem = new TreeItem(tree, 0); iItem.setText("TreeItem (0) -" + i); iItem.setImage(eclipseTree1); TreeItem jItem = null; for (int j = 0; j < 1; j++) { jItem = new TreeItem(iItem, 0); jItem.setText("TreeItem (1) -" + j); jItem.setImage(eclipseTree2); jItem.setExpanded(true); } tree.select(jItem); } // Shell Location Monitor primary = display.getPrimaryMonitor(); Rectangle bounds = primary.getBounds(); Rectangle rect = shell.getBounds(); int x = bounds.x + (bounds.width - rect.width) / 2; int y = bounds.y + (bounds.height - rect.height) / 2; shell.setLocation(x, y); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:Snippet135.java
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setText("SWT and Swing/AWT Example"); Listener exitListener = new Listener() { public void handleEvent(Event e) { MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION); dialog.setText("Question"); dialog.setMessage("Exit?"); if (e.type == SWT.Close) e.doit = false;/* w ww .j a v a2 s . c o m*/ if (dialog.open() != SWT.OK) return; shell.dispose(); } }; Listener aboutListener = new Listener() { public void handleEvent(Event e) { final Shell s = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); s.setText("About"); GridLayout layout = new GridLayout(1, false); layout.verticalSpacing = 20; layout.marginHeight = layout.marginWidth = 10; s.setLayout(layout); Label label = new Label(s, SWT.NONE); label.setText("SWT and AWT Example."); Button button = new Button(s, SWT.PUSH); button.setText("OK"); GridData data = new GridData(); data.horizontalAlignment = GridData.CENTER; button.setLayoutData(data); button.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { s.dispose(); } }); s.pack(); Rectangle parentBounds = shell.getBounds(); Rectangle bounds = s.getBounds(); int x = parentBounds.x + (parentBounds.width - bounds.width) / 2; int y = parentBounds.y + (parentBounds.height - bounds.height) / 2; s.setLocation(x, y); s.open(); while (!s.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } }; shell.addListener(SWT.Close, exitListener); Menu mb = new Menu(shell, SWT.BAR); MenuItem fileItem = new MenuItem(mb, SWT.CASCADE); fileItem.setText("&File"); Menu fileMenu = new Menu(shell, SWT.DROP_DOWN); fileItem.setMenu(fileMenu); MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH); exitItem.setText("&Exit\tCtrl+X"); exitItem.setAccelerator(SWT.CONTROL + 'X'); exitItem.addListener(SWT.Selection, exitListener); MenuItem aboutItem = new MenuItem(fileMenu, SWT.PUSH); aboutItem.setText("&About\tCtrl+A"); aboutItem.setAccelerator(SWT.CONTROL + 'A'); aboutItem.addListener(SWT.Selection, aboutListener); shell.setMenuBar(mb); RGB color = shell.getBackground().getRGB(); Label separator1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); Label locationLb = new Label(shell, SWT.NONE); locationLb.setText("Location:"); Composite locationComp = new Composite(shell, SWT.EMBEDDED); ToolBar toolBar = new ToolBar(shell, SWT.FLAT); ToolItem exitToolItem = new ToolItem(toolBar, SWT.PUSH); exitToolItem.setText("&Exit"); exitToolItem.addListener(SWT.Selection, exitListener); ToolItem aboutToolItem = new ToolItem(toolBar, SWT.PUSH); aboutToolItem.setText("&About"); aboutToolItem.addListener(SWT.Selection, aboutListener); Label separator2 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); final Composite comp = new Composite(shell, SWT.NONE); final Tree fileTree = new Tree(comp, SWT.SINGLE | SWT.BORDER); Sash sash = new Sash(comp, SWT.VERTICAL); Composite tableComp = new Composite(comp, SWT.EMBEDDED); Label separator3 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); Composite statusComp = new Composite(shell, SWT.EMBEDDED); java.awt.Frame locationFrame = SWT_AWT.new_Frame(locationComp); final java.awt.TextField locationText = new java.awt.TextField(); locationFrame.add(locationText); java.awt.Frame fileTableFrame = SWT_AWT.new_Frame(tableComp); java.awt.Panel panel = new java.awt.Panel(new java.awt.BorderLayout()); fileTableFrame.add(panel); final JTable fileTable = new JTable(new FileTableModel(null)); fileTable.setDoubleBuffered(true); fileTable.setShowGrid(false); fileTable.createDefaultColumnsFromModel(); JScrollPane scrollPane = new JScrollPane(fileTable); panel.add(scrollPane); java.awt.Frame statusFrame = SWT_AWT.new_Frame(statusComp); statusFrame.setBackground(new java.awt.Color(color.red, color.green, color.blue)); final java.awt.Label statusLabel = new java.awt.Label(); statusFrame.add(statusLabel); statusLabel.setText("Select a file"); sash.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { if (e.detail == SWT.DRAG) return; GridData data = (GridData) fileTree.getLayoutData(); Rectangle trim = fileTree.computeTrim(0, 0, 0, 0); data.widthHint = e.x - trim.width; comp.layout(); } }); File[] roots = File.listRoots(); for (int i = 0; i < roots.length; i++) { File file = roots[i]; TreeItem treeItem = new TreeItem(fileTree, SWT.NONE); treeItem.setText(file.getAbsolutePath()); treeItem.setData(file); new TreeItem(treeItem, SWT.NONE); } fileTree.addListener(SWT.Expand, new Listener() { public void handleEvent(Event e) { TreeItem item = (TreeItem) e.item; if (item == null) return; if (item.getItemCount() == 1) { TreeItem firstItem = item.getItems()[0]; if (firstItem.getData() != null) return; firstItem.dispose(); } else { return; } File root = (File) item.getData(); File[] files = root.listFiles(); if (files == null) return; for (int i = 0; i < files.length; i++) { File file = files[i]; if (file.isDirectory()) { TreeItem treeItem = new TreeItem(item, SWT.NONE); treeItem.setText(file.getName()); treeItem.setData(file); new TreeItem(treeItem, SWT.NONE); } } } }); fileTree.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { TreeItem item = (TreeItem) e.item; if (item == null) return; final File root = (File) item.getData(); EventQueue.invokeLater(new Runnable() { public void run() { statusLabel.setText(root.getAbsolutePath()); locationText.setText(root.getAbsolutePath()); fileTable.setModel(new FileTableModel(root.listFiles())); } }); } }); GridLayout layout = new GridLayout(4, false); layout.marginWidth = layout.marginHeight = 0; layout.horizontalSpacing = layout.verticalSpacing = 1; shell.setLayout(layout); GridData data; data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 4; separator1.setLayoutData(data); data = new GridData(); data.horizontalSpan = 1; data.horizontalIndent = 10; locationLb.setLayoutData(data); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 2; data.heightHint = locationText.getPreferredSize().height; locationComp.setLayoutData(data); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 1; toolBar.setLayoutData(data); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 4; separator2.setLayoutData(data); data = new GridData(GridData.FILL_BOTH); data.horizontalSpan = 4; comp.setLayoutData(data); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 4; separator3.setLayoutData(data); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 4; data.heightHint = statusLabel.getPreferredSize().height; statusComp.setLayoutData(data); layout = new GridLayout(3, false); layout.marginWidth = layout.marginHeight = 0; layout.horizontalSpacing = layout.verticalSpacing = 1; comp.setLayout(layout); data = new GridData(GridData.FILL_VERTICAL); data.widthHint = 200; fileTree.setLayoutData(data); data = new GridData(GridData.FILL_VERTICAL); sash.setLayoutData(data); data = new GridData(GridData.FILL_BOTH); tableComp.setLayoutData(data); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:PaintExample.java
/** * Invokes as a standalone program.// w w w . ja va2 s .c o m */ public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText(getResourceString("window.title")); shell.setLayout(new GridLayout()); PaintExample instance = new PaintExample(shell); instance.createToolBar(shell); Composite composite = new Composite(shell, SWT.NONE); composite.setLayout(new FillLayout()); composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); instance.createGUI(composite); instance.setDefaults(); setShellSize(display, shell); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } instance.dispose(); }