List of usage examples for org.eclipse.swt.widgets Composite setLayout
public void setLayout(Layout layout)
From source file:TableColumnFirstFix.java
public static void main(String[] args) { int rowCount = 40; int columnCount = 15; final Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); Composite parent = new Composite(shell, SWT.BORDER); GridLayout layout = new GridLayout(2, false); layout.marginWidth = layout.marginHeight = layout.horizontalSpacing = 0; parent.setLayout(layout); final Table leftTable = new Table(parent, SWT.MULTI | SWT.FULL_SELECTION); leftTable.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true)); leftTable.setHeaderVisible(true);/*w w w . j ava 2s .co m*/ final Table rightTable = new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION); rightTable.setHeaderVisible(true); GridData table2Data = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2); rightTable.setLayoutData(table2Data); // Create columns TableColumn column1 = new TableColumn(leftTable, SWT.NONE); column1.setText("Name"); column1.setWidth(150); for (int i = 0; i < columnCount; i++) { TableColumn column = new TableColumn(rightTable, SWT.NONE); column.setText("Value " + i); column.setWidth(200); } // Create rows for (int i = 0; i < rowCount; i++) { TableItem item = new TableItem(leftTable, SWT.NONE); item.setText("item " + i); item = new TableItem(rightTable, SWT.NONE); for (int j = 0; j < columnCount; j++) { item.setText(j, "Item " + i + " value @ " + j); } } // Make selection the same in both tables leftTable.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { rightTable.setSelection(leftTable.getSelectionIndices()); } }); rightTable.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { leftTable.setSelection(rightTable.getSelectionIndices()); } }); // On Windows, the selection is gray if the table does not have focus. // To make both tables appear in focus, draw teh selection background here. // This part only works on version 3.2 or later. Listener eraseListener = new Listener() { public void handleEvent(Event event) { if ((event.detail & SWT.SELECTED) != 0) { GC gc = event.gc; Rectangle rect = event.getBounds(); gc.setForeground(display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT)); gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_SELECTION)); gc.fillRectangle(rect); event.detail &= ~SWT.SELECTED; } } }; leftTable.addListener(SWT.EraseItem, eraseListener); rightTable.addListener(SWT.EraseItem, eraseListener); // Make vertical scrollbars scroll together ScrollBar vBarLeft = leftTable.getVerticalBar(); vBarLeft.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { rightTable.setTopIndex(leftTable.getTopIndex()); } }); ScrollBar vBarRight = rightTable.getVerticalBar(); vBarRight.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { leftTable.setTopIndex(rightTable.getTopIndex()); } }); // Horizontal bar on second table takes up a little extra space. // To keep vertical scroll bars in sink, force table1 to end above // horizontal scrollbar ScrollBar hBarRight = rightTable.getHorizontalBar(); Label spacer = new Label(parent, SWT.NONE); GridData spacerData = new GridData(); spacerData.heightHint = hBarRight.getSize().y; spacer.setVisible(false); parent.setBackground(leftTable.getBackground()); shell.setSize(600, 400); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
From source file:org.eclipse.swt.snippets.Snippet223.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); shell.setText("ExpandBar Example"); ExpandBar bar = new ExpandBar(shell, SWT.V_SCROLL); Image image = display.getSystemImage(SWT.ICON_QUESTION); // First item Composite composite = new Composite(bar, SWT.NONE); GridLayout layout = new GridLayout(); layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10; layout.verticalSpacing = 10;/*w w w .j a v a 2 s . co m*/ composite.setLayout(layout); Button button = new Button(composite, SWT.PUSH); button.setText("SWT.PUSH"); button = new Button(composite, SWT.RADIO); button.setText("SWT.RADIO"); button = new Button(composite, SWT.CHECK); button.setText("SWT.CHECK"); button = new Button(composite, SWT.TOGGLE); button.setText("SWT.TOGGLE"); ExpandItem item0 = new ExpandItem(bar, SWT.NONE, 0); item0.setText("What is your favorite button"); item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y); item0.setControl(composite); item0.setImage(image); // Second item composite = new Composite(bar, SWT.NONE); layout = new GridLayout(2, false); layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10; layout.verticalSpacing = 10; composite.setLayout(layout); Label label = new Label(composite, SWT.NONE); label.setImage(display.getSystemImage(SWT.ICON_ERROR)); label = new Label(composite, SWT.NONE); label.setText("SWT.ICON_ERROR"); label = new Label(composite, SWT.NONE); label.setImage(display.getSystemImage(SWT.ICON_INFORMATION)); label = new Label(composite, SWT.NONE); label.setText("SWT.ICON_INFORMATION"); label = new Label(composite, SWT.NONE); label.setImage(display.getSystemImage(SWT.ICON_WARNING)); label = new Label(composite, SWT.NONE); label.setText("SWT.ICON_WARNING"); label = new Label(composite, SWT.NONE); label.setImage(display.getSystemImage(SWT.ICON_QUESTION)); label = new Label(composite, SWT.NONE); label.setText("SWT.ICON_QUESTION"); ExpandItem item1 = new ExpandItem(bar, SWT.NONE, 1); item1.setText("What is your favorite icon"); item1.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y); item1.setControl(composite); item1.setImage(image); // Third item composite = new Composite(bar, SWT.NONE); layout = new GridLayout(2, true); layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10; layout.verticalSpacing = 10; composite.setLayout(layout); label = new Label(composite, SWT.NONE); label.setText("Scale"); new Scale(composite, SWT.NONE); label = new Label(composite, SWT.NONE); label.setText("Spinner"); new Spinner(composite, SWT.BORDER); label = new Label(composite, SWT.NONE); label.setText("Slider"); new Slider(composite, SWT.NONE); ExpandItem item2 = new ExpandItem(bar, SWT.NONE, 2); item2.setText("What is your favorite range widget"); item2.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y); item2.setControl(composite); item2.setImage(image); item1.setExpanded(true); bar.setSpacing(8); shell.setSize(400, 350); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } image.dispose(); 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);//from w w w . j a va 2 s . 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:org.eclipse.swt.snippets.Snippet166.java
public static void main(String[] args) { Display display = new Display(); Image image1 = display.getSystemImage(SWT.ICON_WORKING); Image image2 = display.getSystemImage(SWT.ICON_QUESTION); Image image3 = display.getSystemImage(SWT.ICON_ERROR); Shell shell = new Shell(display); shell.setText("Snippet 166"); shell.setLayout(new FillLayout()); final ScrolledComposite scrollComposite = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.BORDER); final Composite parent = new Composite(scrollComposite, SWT.NONE); for (int i = 0; i <= 50; i++) { Label label = new Label(parent, SWT.NONE); if (i % 3 == 0) label.setImage(image1);/*from w w w .java2 s .c om*/ if (i % 3 == 1) label.setImage(image2); if (i % 3 == 2) label.setImage(image3); } RowLayout layout = new RowLayout(SWT.HORIZONTAL); layout.wrap = true; parent.setLayout(layout); scrollComposite.setContent(parent); scrollComposite.setExpandVertical(true); scrollComposite.setExpandHorizontal(true); scrollComposite.addControlListener(ControlListener.controlResizedAdapter(e -> { Rectangle r = scrollComposite.getClientArea(); scrollComposite.setMinSize(parent.computeSize(r.width, SWT.DEFAULT)); })); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
From source file:ScrolledCompositeCreate.java
public static void main(String[] args) { Display display = new Display(); Image image1 = display.getSystemImage(SWT.ICON_WORKING); Image image2 = display.getSystemImage(SWT.ICON_QUESTION); Image image3 = display.getSystemImage(SWT.ICON_ERROR); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final ScrolledComposite scrollComposite = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.BORDER); final Composite parent = new Composite(scrollComposite, SWT.NONE); for (int i = 0; i <= 50; i++) { Label label = new Label(parent, SWT.NONE); if (i % 3 == 0) label.setImage(image1);//from ww w . j av a 2 s . co m if (i % 3 == 1) label.setImage(image2); if (i % 3 == 2) label.setImage(image3); } RowLayout layout = new RowLayout(SWT.HORIZONTAL); layout.wrap = true; parent.setLayout(layout); scrollComposite.setContent(parent); scrollComposite.setExpandVertical(true); scrollComposite.setExpandHorizontal(true); scrollComposite.addControlListener(new ControlAdapter() { public void controlResized(ControlEvent e) { Rectangle r = scrollComposite.getClientArea(); scrollComposite.setMinSize(parent.computeSize(r.width, SWT.DEFAULT)); } }); 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);/* w w w .ja v a 2 s. co m*/ 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:PaintExample.java
/** * Invokes as a standalone program.// w w w. jav a 2 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(); }
From source file:ExampleGUI.java
public static void main(String[] args) { // create a shell... Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); shell.setText("KTable examples"); // put a tab folder in it... TabFolder tabFolder = new TabFolder(shell, SWT.NONE); // Item 1: a Text Table TabItem item1 = new TabItem(tabFolder, SWT.NONE); item1.setText("Text Table"); Composite comp1 = new Composite(tabFolder, SWT.NONE); item1.setControl(comp1);//w w w. ja v a 2 s . c o m comp1.setLayout(new FillLayout()); // put a table in tabItem1... KTable table = new KTable(comp1, SWT.V_SCROLL | SWT.H_SCROLL); // table.setRowSelectionMode(true); table.setMultiSelectionMode(true); table.setModel(new KTableModelExample()); table.addCellSelectionListener(new KTableCellSelectionListener() { public void cellSelected(int col, int row, int statemask) { System.out.println("Cell [" + col + ";" + row + "] selected."); } public void fixedCellSelected(int col, int row, int statemask) { System.out.println("Header [" + col + ";" + row + "] selected."); } }); table.addCellResizeListener(new KTableCellResizeListener() { public void columnResized(int col, int newWidth) { System.out.println("Column " + col + " resized to " + newWidth); } public void rowResized(int newHeight) { System.out.println("Rows resized to " + newHeight); } }); // Item 2: a Color Palette TabItem item2 = new TabItem(tabFolder, SWT.NONE); item2.setText("Color Palette"); Composite comp2 = new Composite(tabFolder, SWT.NONE); item2.setControl(comp2); comp2.setLayout(new FillLayout()); // put a table in tabItem2... final KTable table2 = new KTable(comp2, SWT.NONE); table2.setModel(new PaletteExampleModel()); table2.setRowSelectionMode(false); table2.setMultiSelectionMode(false); final Label label = new Label(comp2, SWT.NONE); label.setText("Click a cell..."); table2.addCellSelectionListener(new KTableCellSelectionAdapter() { public void cellSelected(int col, int row, int statemask) { RGB rgb = (RGB) table2.getModel().getContentAt(col, row); label.setText("R: " + rgb.red + "\nG: " + rgb.green + "\nB: " + rgb.blue); } }); // Item 3: Town table TabItem item3 = new TabItem(tabFolder, SWT.NONE); item3.setText("Towns"); Composite comp3 = new Composite(tabFolder, SWT.NONE); item3.setControl(comp3); comp3.setLayout(new FillLayout()); // put a table in tabItem3... final KTable table3 = new KTable(comp3, SWT.FLAT | SWT.H_SCROLL); table3.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_LIST_BACKGROUND)); table3.setModel(new TownExampleModel()); table3.setRowSelectionMode(true); table3.setMultiSelectionMode(false); // display the shell... shell.setSize(600, 600); 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. jav a 2 s.com 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 .j a va 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(); }