List of usage examples for org.eclipse.swt.widgets Label setLayoutData
public void setLayoutData(Object layoutData)
From source file:StatusLineUpdateWhenMenuItemArmed.java
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); FormLayout layout = new FormLayout(); shell.setLayout(layout);/*from ww w .j ava 2 s .co m*/ final Label label = new Label(shell, SWT.BORDER); Listener armListener = new Listener() { public void handleEvent(Event event) { MenuItem item = (MenuItem) event.widget; label.setText(item.getText()); label.update(); } }; Listener showListener = new Listener() { public void handleEvent(Event event) { Menu menu = (Menu) event.widget; MenuItem item = menu.getParentItem(); if (item != null) { label.setText(item.getText()); label.update(); } } }; Listener hideListener = new Listener() { public void handleEvent(Event event) { label.setText(""); label.update(); } }; FormData labelData = new FormData(); labelData.left = new FormAttachment(0); labelData.right = new FormAttachment(100); labelData.bottom = new FormAttachment(100); label.setLayoutData(labelData); Menu menuBar = new Menu(shell, SWT.BAR); shell.setMenuBar(menuBar); MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE); fileItem.setText("File"); fileItem.addListener(SWT.Arm, armListener); MenuItem editItem = new MenuItem(menuBar, SWT.CASCADE); editItem.setText("Edit"); editItem.addListener(SWT.Arm, armListener); Menu fileMenu = new Menu(shell, SWT.DROP_DOWN); fileMenu.addListener(SWT.Hide, hideListener); fileMenu.addListener(SWT.Show, showListener); fileItem.setMenu(fileMenu); String[] fileStrings = { "New", "Close", "Exit" }; for (int i = 0; i < fileStrings.length; i++) { MenuItem item = new MenuItem(fileMenu, SWT.PUSH); item.setText(fileStrings[i]); item.addListener(SWT.Arm, armListener); } Menu editMenu = new Menu(shell, SWT.DROP_DOWN); editMenu.addListener(SWT.Hide, hideListener); editMenu.addListener(SWT.Show, showListener); String[] editStrings = { "Cut", "Copy", "Paste" }; editItem.setMenu(editMenu); for (int i = 0; i < editStrings.length; i++) { MenuItem item = new MenuItem(editMenu, SWT.PUSH); item.setText(editStrings[i]); item.addListener(SWT.Arm, armListener); } 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);//from w w w. ja v a 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 -> {/*from w w w . j a v a 2 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 v a2s .com*/ 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:ControlListenerExample.java
/** * Creates the main window's contents// w w w . ja va 2s . co m * * @param shell the main window * @param image the image */ private void createContents(Shell shell, Image image) { shell.setLayout(new GridLayout()); // Create a label to hold the image Label label = new Label(shell, SWT.NONE); label.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING)); label.setImage(image); shell.setData(label); // Add the listener shell.addControlListener(new ControlAdapter() { public void controlResized(ControlEvent event) { // Get the event source (the shell) Shell shell = (Shell) event.getSource(); // Get the source's data (the label) Label label = (Label) shell.getData(); // Determine how big the shell should be to fit the image Rectangle rect = shell.getClientArea(); ImageData data = label.getImage().getImageData(); // If the shell is too small, hide the image if (rect.width < data.width || rect.height < data.height) { shell.setText("Too small."); label.setText("I'm melting!"); } else { // He fits! shell.setText("Happy Guy Fits!"); label.setImage(label.getImage()); } } }); }
From source file:ShowPrograms.java
/** * Creates the main window's contents//from w w w . ja v a2 s.c o m * * @param shell the main window */ private void createContents(Shell shell) { shell.setLayout(new GridLayout(2, false)); // Create the label and combo for the extensions new Label(shell, SWT.NONE).setText("Extension:"); Combo extensionsCombo = new Combo(shell, SWT.BORDER | SWT.READ_ONLY); extensionsCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // Create the label and the new Label(shell, SWT.NONE).setText("Program:"); final Label programName = new Label(shell, SWT.NONE); programName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // Fill the combo with the extensions on the system String[] extensions = Program.getExtensions(); for (int i = 0, n = extensions.length; i < n; i++) { extensionsCombo.add(extensions[i]); } // Add a handler to get the selected extension, look up the associated // program, and display the program's name extensionsCombo.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { Combo combo = (Combo) event.widget; // Get the program for the extension Program program = Program.findProgram(combo.getText()); // Display the program's name programName.setText(program == null ? "(None)" : program.getName()); } }); // Create a list box to show all the programs on the system List allPrograms = new List(shell, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); GridData data = new GridData(GridData.FILL_BOTH); data.horizontalSpan = 2; allPrograms.setLayoutData(data); // Put all the known programs into the list box Program[] programs = Program.getPrograms(); for (int i = 0, n = programs.length; i < n; i++) { String name = programs[i].getName(); allPrograms.add(name); allPrograms.setData(name, programs[i]); } // Add a field for a data file new Label(shell, SWT.NONE).setText("Data File:"); final Text dataFile = new Text(shell, SWT.BORDER); dataFile.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // Double-clicking a program in the list launches the program allPrograms.addMouseListener(new MouseAdapter() { public void mouseDoubleClick(MouseEvent event) { List list = (List) event.widget; if (list.getSelectionCount() > 0) { String programName = list.getSelection()[0]; Program program = (Program) list.getData(programName); program.execute(dataFile.getText()); } } }); // Let them use launch instead of execute Button launch = new Button(shell, SWT.PUSH); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 2; launch.setLayoutData(data); launch.setText("Use Program.launch() instead of Program.execute()"); launch.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { // Use launch Program.launch(dataFile.getText()); } }); }
From source file:InputDialog.java
private void createContents(final Shell shell) { shell.setLayout(new GridLayout(2, true)); Label label = new Label(shell, SWT.NONE); label.setText(message);// www. j av a 2s. c o m GridData data = new GridData(); data.horizontalSpan = 2; label.setLayoutData(data); final Text text = new Text(shell, SWT.BORDER); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 2; text.setLayoutData(data); Button ok = new Button(shell, SWT.PUSH); ok.setText("OK"); data = new GridData(GridData.FILL_HORIZONTAL); ok.setLayoutData(data); ok.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { input = text.getText(); shell.close(); } }); Button cancel = new Button(shell, SWT.PUSH); cancel.setText("Cancel"); data = new GridData(GridData.FILL_HORIZONTAL); cancel.setLayoutData(data); cancel.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { input = null; shell.close(); } }); shell.setDefaultButton(ok); }
From source file:ShowMessageBox.java
/** * Creates the main window's contents//from w ww .jav a 2s . co m * * @param shell the parent shell */ private void createContents(final Shell shell) { shell.setLayout(new GridLayout(2, false)); // Create the dropdown to allow icon selection new Label(shell, SWT.NONE).setText("Icon:"); final Combo icons = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY); for (int i = 0, n = ICONS.length; i < n; i++) icons.add(ICONS[i]); icons.select(0); // Create the dropdown to allow button selection new Label(shell, SWT.NONE).setText("Buttons:"); final Combo buttons = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY); for (int i = 0, n = BUTTONS.length; i < n; i++) buttons.add(BUTTONS[i]); buttons.select(0); // Create the entry field for the message new Label(shell, SWT.NONE).setText("Message:"); final Text message = new Text(shell, SWT.BORDER); message.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // Create the label to show the return from the open call new Label(shell, SWT.NONE).setText("Return:"); final Label returnVal = new Label(shell, SWT.NONE); returnVal.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // Create the button and event handler // to display the message box Button button = new Button(shell, SWT.PUSH); button.setText("Show Message"); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { // Clear any previously returned value returnVal.setText(""); // This will hold the style to pass to the MessageBox constructor int style = 0; // Determine which icon was selected and // add it to the style switch (icons.getSelectionIndex()) { case 0: style |= SWT.ICON_ERROR; break; case 1: style |= SWT.ICON_INFORMATION; break; case 2: style |= SWT.ICON_QUESTION; break; case 3: style |= SWT.ICON_WARNING; break; case 4: style |= SWT.ICON_WORKING; break; } // Determine which set of buttons was selected // and add it to the style switch (buttons.getSelectionIndex()) { case 0: style |= SWT.OK; break; case 1: style |= SWT.OK | SWT.CANCEL; break; case 2: style |= SWT.YES | SWT.NO; break; case 3: style |= SWT.YES | SWT.NO | SWT.CANCEL; break; case 4: style |= SWT.RETRY | SWT.CANCEL; break; case 5: style |= SWT.ABORT | SWT.RETRY | SWT.IGNORE; break; } // Display the message box MessageBox mb = new MessageBox(shell, style); mb.setText("Message from SWT"); mb.setMessage(message.getText()); int val = mb.open(); String valString = ""; switch (val) // val contains the constant of the selected button { case SWT.OK: valString = "SWT.OK"; break; case SWT.CANCEL: valString = "SWT.CANCEL"; break; case SWT.YES: valString = "SWT.YES"; break; case SWT.NO: valString = "SWT.NO"; break; case SWT.RETRY: valString = "SWT.RETRY"; break; case SWT.ABORT: valString = "SWT.ABORT"; break; case SWT.IGNORE: valString = "SWT.IGNORE"; break; } returnVal.setText(valString); } }); }
From source file:SendMessage.java
/** * Creates the main window's contents/*w ww .j a v a2 s . com*/ * * @param parent the main window * @return Control */ protected Control createContents(Composite parent) { Composite composite = new Composite(parent, SWT.NONE); composite.setLayout(new GridLayout(5, true)); // Create a big text box for the message text final Text text = new Text(composite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL); GridData data = new GridData(GridData.FILL_BOTH); data.horizontalSpan = 5; text.setLayoutData(data); // Create the Confirm button Button confirm = new Button(composite, SWT.PUSH); confirm.setText("Confirm"); confirm.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // Create the Error button Button error = new Button(composite, SWT.PUSH); error.setText("Error"); error.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // Create the Information button Button information = new Button(composite, SWT.PUSH); information.setText("Information"); information.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // Create the Question button Button question = new Button(composite, SWT.PUSH); question.setText("Question"); question.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // Create the Warning button Button warning = new Button(composite, SWT.PUSH); warning.setText("Warning"); warning.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // Create the label to display the return value final Label label = new Label(composite, SWT.NONE); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 5; label.setLayoutData(data); // Save ourselves some typing final Shell shell = parent.getShell(); // Display a Confirmation dialog confirm.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { boolean b = MessageDialog.openConfirm(shell, "Confirm", text.getText()); label.setText("Returned " + Boolean.toString(b)); } }); // Display an Error dialog error.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { MessageDialog.openError(shell, "Error", text.getText()); label.setText("Returned void"); } }); // Display an Information dialog information.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { MessageDialog.openInformation(shell, "Information", text.getText()); label.setText("Returned void"); } }); // Display a Question dialog question.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { boolean b = MessageDialog.openQuestion(shell, "Question", text.getText()); label.setText("Returned " + Boolean.toString(b)); } }); // Display a Warning dialog warning.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { MessageDialog.openWarning(shell, "Warning", text.getText()); label.setText("Returned void"); } }); return composite; }
From source file:org.eclipse.swt.examples.paint.TextTool.java
/** * Handles a mouseDown event./*from ww w .j a v a 2 s. com*/ * * @param event the mouse event detail information */ @Override public void mouseDown(MouseEvent event) { if (event.button == 1) { // draw with left mouse button getPaintSurface().commitRubberbandSelection(); } else { // set text with right mouse button getPaintSurface().clearRubberbandSelection(); Shell shell = getPaintSurface().getShell(); final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); dialog.setText(PaintExample.getResourceString("tool.Text.dialog.title")); dialog.setLayout(new GridLayout()); Label label = new Label(dialog, SWT.NONE); label.setText(PaintExample.getResourceString("tool.Text.dialog.message")); label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); final Text field = new Text(dialog, SWT.SINGLE | SWT.BORDER); field.setText(drawText); field.selectAll(); field.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); Composite buttons = new Composite(dialog, SWT.NONE); GridLayout layout = new GridLayout(2, true); layout.marginWidth = 0; buttons.setLayout(layout); buttons.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false)); Button ok = new Button(buttons, SWT.PUSH); ok.setText(PaintExample.getResourceString("OK")); ok.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); ok.addSelectionListener(widgetSelectedAdapter(e -> { drawText = field.getText(); dialog.dispose(); })); Button cancel = new Button(buttons, SWT.PUSH); cancel.setText(PaintExample.getResourceString("Cancel")); cancel.addSelectionListener(widgetSelectedAdapter(e -> dialog.dispose())); dialog.setDefaultButton(ok); dialog.pack(); dialog.open(); Display display = dialog.getDisplay(); while (!shell.isDisposed() && !dialog.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } }