List of usage examples for org.eclipse.swt.widgets Tree setLayoutData
public void setLayoutData(Object layoutData)
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 ww.j a va 2 s. c o m*/ } 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: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 ava 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.Snippet266.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Snippet 266"); shell.setLayout(new GridLayout(2, true)); TabFolder tabFolder = new TabFolder(shell, SWT.NONE); tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1)); TabItem item = new TabItem(tabFolder, SWT.NONE); item.setText("Widget"); Composite composite = new Composite(tabFolder, SWT.NONE); composite.setLayout(new GridLayout()); Tree tree = new Tree(composite, SWT.BORDER); item.setControl(composite);//from w w w .j av a 2s . c o m tree.setHeaderVisible(true); tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); TreeColumn column1 = new TreeColumn(tree, SWT.NONE); column1.setText("Standard"); TreeColumn column2 = new TreeColumn(tree, SWT.NONE); column2.setText("Widget"); TreeItem branch = new TreeItem(tree, SWT.NONE); branch.setText(new String[] { "Efficient", "Portable" }); TreeItem leaf = new TreeItem(branch, SWT.NONE); leaf.setText(new String[] { "Cross", "Platform" }); branch.setExpanded(true); branch = new TreeItem(tree, SWT.NONE); branch.setText(new String[] { "Native", "Controls" }); leaf = new TreeItem(branch, SWT.NONE); leaf.setText(new String[] { "Cross", "Platform" }); branch = new TreeItem(tree, SWT.NONE); branch.setText(new String[] { "Cross", "Platform" }); column1.pack(); column2.pack(); item = new TabItem(tabFolder, SWT.NONE); item.setText("Toolkit"); Button button = new Button(shell, SWT.CHECK); button.setText("Totally"); button.setSelection(true); button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false)); button = new Button(shell, SWT.PUSH); button.setText("Awesome"); button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false)); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet360.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Snippet 360"); shell.setLayout(new GridLayout()); // create a a tree with 3 columns and fill with data final Tree tree = new Tree(shell, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION); tree.setLayoutData(new GridData(GridData.FILL_BOTH)); tree.setHeaderVisible(true);/* ww w . j av a 2 s . c om*/ TreeColumn column1 = new TreeColumn(tree, SWT.NONE); TreeColumn column2 = new TreeColumn(tree, SWT.NONE); TreeColumn column3 = new TreeColumn(tree, SWT.NONE); for (int i = 0; i < 9; i++) { TreeItem item = new TreeItem(tree, SWT.NONE); item.setText(new String[] { "root " + i + " 0", "root " + i + " 1", "root " + i + " 2" }); for (int j = 0; j < 9; j++) { TreeItem item2 = new TreeItem(item, SWT.NONE); item2.setText(new String[] { "child " + j + " 0", "child " + j + " 1", "child " + j + " 2" }); TreeItem current = item2; for (int k = 0; k < 5; k++) { TreeItem item3 = new TreeItem(current, SWT.NONE); item3.setText(new String[] { "descendent " + k + " 0", "descendent " + k + " 1", "descendent " + k + " 2" }); current = item3; } } } column1.setWidth(200); column2.setWidth(100); column3.setWidth(100); // create a TreeCursor to navigate around the tree final TreeCursor cursor = new TreeCursor(tree, SWT.NONE); // create an editor to edit the cell when the user hits "ENTER" // while over a cell in the tree final ControlEditor editor = new ControlEditor(cursor); editor.grabHorizontal = true; editor.grabVertical = true; cursor.addSelectionListener(new SelectionAdapter() { // when the TreeEditor is over a cell, select the corresponding row // in the tree @Override public void widgetSelected(SelectionEvent e) { tree.setSelection(new TreeItem[] { cursor.getRow() }); } // when the user hits "ENTER" in the TreeCursor, pop up a text // editor so that they can change the text of the cell @Override public void widgetDefaultSelected(SelectionEvent e) { final Text text = new Text(cursor, SWT.NONE); TreeItem row = cursor.getRow(); int column = cursor.getColumn(); text.setText(row.getText(column)); text.addKeyListener(keyPressedAdapter(event -> { // close the text editor and copy the data over // when the user hits "ENTER" if (event.character == SWT.CR) { TreeItem localRow = cursor.getRow(); int localColumn = cursor.getColumn(); localRow.setText(localColumn, text.getText()); text.dispose(); } // close the text editor when the user hits "ESC" if (event.character == SWT.ESC) { text.dispose(); } })); editor.setEditor(text); text.setFocus(); } }); // Hide the TreeCursor when the user hits the "MOD1" or "MOD2" key. // This allows the user to select multiple items in the tree. cursor.addKeyListener(keyPressedAdapter(e -> { if (e.keyCode == SWT.MOD1 || e.keyCode == SWT.MOD2 || (e.stateMask & SWT.MOD1) != 0 || (e.stateMask & SWT.MOD2) != 0) { cursor.setVisible(false); } })); // Show the TreeCursor when the user releases the "MOD2" or "MOD1" key. // This signals the end of the multiple selection task. tree.addKeyListener(new KeyListener() { @Override public void keyReleased(KeyEvent e) { if (e.keyCode == SWT.MOD1 && (e.stateMask & SWT.MOD2) != 0) return; if (e.keyCode == SWT.MOD2 && (e.stateMask & SWT.MOD1) != 0) return; if (e.keyCode != SWT.MOD1 && (e.stateMask & SWT.MOD1) != 0) return; if (e.keyCode != SWT.MOD2 && (e.stateMask & SWT.MOD2) != 0) return; TreeItem[] selection = tree.getSelection(); TreeItem row = (selection.length == 0) ? tree.getItem(tree.indexOf(tree.getTopItem())) : selection[0]; tree.showItem(row); cursor.setSelection(row, cursor.getColumn()); cursor.setVisible(true); cursor.setFocus(); } @Override public void keyPressed(KeyEvent e) { switch (e.keyCode) { case SWT.ARROW_LEFT: case SWT.ARROW_RIGHT: { if ((e.stateMask & SWT.MOD1) != 0) { TreeItem[] selection = tree.getSelection(); if (selection.length > 0) { selection[0].setExpanded(e.keyCode == SWT.ARROW_RIGHT); } break; } } } } }); 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;// ww w .j a va 2 s . 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:SystemFileTree.java
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); 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); 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);// w w w .ja va 2 s. c om 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(); statusLabel.setText(root.getAbsolutePath()); locationText.setText(root.getAbsolutePath()); } }); 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; 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.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 -> {/* w w w . ja va2 s.c o m*/ 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: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 .ja va 2 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:SWTTreeExample.java
private void createContents(Composite composite) { // Set the single-selection tree in the upper left, // the multi-selection tree in the upper right, // and the checkbox tree across the bottom. // To do this, create a 1x2 grid, and in the top // cell, a 2x1 grid. composite.setLayout(new GridLayout(1, true)); Composite top = new Composite(composite, SWT.NONE); GridData data = new GridData(GridData.FILL_BOTH); top.setLayoutData(data);/* w ww. jav a2 s .com*/ top.setLayout(new GridLayout(2, true)); Tree single = new Tree(top, SWT.SINGLE | SWT.BORDER); data = new GridData(GridData.FILL_BOTH); single.setLayoutData(data); fillTree(single); Tree multi = new Tree(top, SWT.MULTI | SWT.BORDER); data = new GridData(GridData.FILL_BOTH); multi.setLayoutData(data); fillTree(multi); Tree check = new Tree(composite, SWT.CHECK | SWT.BORDER); data = new GridData(GridData.FILL_BOTH); check.setLayoutData(data); fillTree(check); }
From source file:org.eclipse.swt.examples.hoverhelp.HoverHelp.java
/** * Creates the example//from ww w . j av a2 s. com */ public void createPartControl(Composite frame) { final ToolTipHandler tooltip = new ToolTipHandler(frame.getShell()); GridLayout layout = new GridLayout(); layout.numColumns = 3; frame.setLayout(layout); String platform = SWT.getPlatform(); String helpKey = "F1"; if (platform.equals("gtk")) helpKey = "Ctrl+F1"; if (platform.equals("cocoa")) helpKey = "Help"; ToolBar bar = new ToolBar(frame, SWT.BORDER); for (int i = 0; i < 5; i++) { ToolItem item = new ToolItem(bar, SWT.PUSH); item.setText(getResourceString("ToolItem.text", new Object[] { Integer.valueOf(i) })); item.setData("TIP_TEXT", getResourceString("ToolItem.tooltip", new Object[] { item.getText(), helpKey })); item.setData("TIP_HELPTEXTHANDLER", (ToolTipHelpTextHandler) widget -> { Item item1 = (Item) widget; return getResourceString("ToolItem.help", new Object[] { item1.getText() }); }); } GridData gridData = new GridData(); gridData.horizontalSpan = 3; bar.setLayoutData(gridData); tooltip.activateHoverHelp(bar); Table table = new Table(frame, SWT.BORDER); for (int i = 0; i < 4; i++) { TableItem item = new TableItem(table, SWT.PUSH); item.setText(getResourceString("Item", new Object[] { Integer.valueOf(i) })); item.setData("TIP_IMAGE", images[hhiInformation]); item.setText(getResourceString("TableItem.text", new Object[] { Integer.valueOf(i) })); item.setData("TIP_TEXT", getResourceString("TableItem.tooltip", new Object[] { item.getText(), helpKey })); item.setData("TIP_HELPTEXTHANDLER", (ToolTipHelpTextHandler) widget -> { Item item1 = (Item) widget; return getResourceString("TableItem.help", new Object[] { item1.getText() }); }); } table.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL)); tooltip.activateHoverHelp(table); Tree tree = new Tree(frame, SWT.BORDER); for (int i = 0; i < 4; i++) { TreeItem item = new TreeItem(tree, SWT.PUSH); item.setText(getResourceString("Item", new Object[] { Integer.valueOf(i) })); item.setData("TIP_IMAGE", images[hhiWarning]); item.setText(getResourceString("TreeItem.text", new Object[] { Integer.valueOf(i) })); item.setData("TIP_TEXT", getResourceString("TreeItem.tooltip", new Object[] { item.getText(), helpKey })); item.setData("TIP_HELPTEXTHANDLER", (ToolTipHelpTextHandler) widget -> { Item item1 = (Item) widget; return getResourceString("TreeItem.help", new Object[] { item1.getText() }); }); } tree.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL)); tooltip.activateHoverHelp(tree); Button button = new Button(frame, SWT.PUSH); button.setText(getResourceString("Hello.text")); button.setData("TIP_TEXT", getResourceString("Hello.tooltip")); tooltip.activateHoverHelp(button); }