void releaseParent_pp() { super.releaseParent_pp(); if (cascade != null) cascade.setMenu(null); if ((style & SWT.BAR) != 0 && this == parent.menuBar) { parent.setMenuBar(null); } }
/** * Creates all the items located in the Search submenu and associate all the menu items with their * appropriate functions. * * @param menuBar Menu the <code>Menu</code> that file contain the Search submenu. */ private void createSearchMenu(Menu menuBar) { // Search menu. MenuItem item = new MenuItem(menuBar, SWT.CASCADE); item.setText(resMessages.getString("Search_menu_title")); Menu searchMenu = new Menu(shell, SWT.DROP_DOWN); item.setMenu(searchMenu); // Search -> Find... item = new MenuItem(searchMenu, SWT.NULL); item.setText(resMessages.getString("Find")); item.setAccelerator(SWT.MOD1 + 'F'); item.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { searchDialog.setMatchCase(false); searchDialog.setMatchWord(false); searchDialog.setSearchDown(true); searchDialog.setSearchString(""); searchDialog.setSelectedSearchArea(0); searchDialog.open(); } }); // Search -> Find Next item = new MenuItem(searchMenu, SWT.NULL); item.setText(resMessages.getString("Find_next")); item.setAccelerator(SWT.F3); item.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { searchDialog.open(); } }); }
/** * Creates all the items located in the Help submenu and associate all the menu items with their * appropriate functions. * * @param menuBar Menu the <code>Menu</code> that file contain the Help submenu. */ private void createHelpMenu(Menu menuBar) { // Help Menu MenuItem item = new MenuItem(menuBar, SWT.CASCADE); item.setText(resMessages.getString("Help_menu_title")); Menu menu = new Menu(shell, SWT.DROP_DOWN); item.setMenu(menu); // Help -> About Text Editor MenuItem subItem = new MenuItem(menu, SWT.NULL); subItem.setText(resMessages.getString("About")); subItem.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { MessageBox box = new MessageBox(shell, SWT.NONE); box.setText(resMessages.getString("About_1") + shell.getText()); box.setMessage(shell.getText() + resMessages.getString("About_2")); box.open(); } }); }
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); shell.setText("Text Editor"); Menu bar = new Menu(shell, SWT.BAR); shell.setMenuBar(bar); MenuItem fileItem = new MenuItem(bar, SWT.CASCADE); fileItem.setText("&File"); Menu fileMenu = new Menu(shell, SWT.DROP_DOWN); fileItem.setMenu(fileMenu); MenuItem saveItem = new MenuItem(fileMenu, SWT.PUSH); saveItem.setText("&Save\tCtrl+S"); saveItem.setAccelerator(SWT.MOD1 + 'S'); saveItem.addListener(SWT.Selection, e -> shell.setModified(false)); MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH); exitItem.setText("Exit"); exitItem.addListener(SWT.Selection, e -> shell.close()); Text text = new Text(shell, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); text.addListener(SWT.Modify, e -> shell.setModified(true)); shell.addListener( SWT.Close, e -> { if (shell.getModified()) { MessageBox box = new MessageBox(shell, SWT.PRIMARY_MODAL | SWT.OK | SWT.CANCEL); box.setText(shell.getText()); box.setMessage("You have unsaved changes, do you want to exit?"); e.doit = box.open() == SWT.OK; } }); shell.setLayout(new FillLayout()); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
/** * Creates all the items located in the Edit submenu and associate all the menu items with their * appropriate functions. * * @param menuBar Menu the <code>Menu</code> that file contain the Edit submenu. * @see #createSortMenu() */ private MenuItem createEditMenu(Menu menuBar) { // Edit menu. MenuItem item = new MenuItem(menuBar, SWT.CASCADE); item.setText(resAddressBook.getString("Edit_menu_title")); Menu menu = new Menu(shell, SWT.DROP_DOWN); item.setMenu(menu); /** Add a listener to handle enabling and disabling some items in the Edit submenu. */ menu.addMenuListener( new MenuAdapter() { @Override public void menuShown(MenuEvent e) { Menu menu = (Menu) e.widget; MenuItem[] items = menu.getItems(); int count = table.getSelectionCount(); items[0].setEnabled(count != 0); // edit items[1].setEnabled(count != 0); // copy items[2].setEnabled(copyBuffer != null); // paste items[3].setEnabled(count != 0); // delete items[5].setEnabled(table.getItemCount() != 0); // sort } }); // Edit -> Edit MenuItem subItem = new MenuItem(menu, SWT.PUSH); subItem.setText(resAddressBook.getString("Edit")); subItem.setAccelerator(SWT.MOD1 + 'E'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { TableItem[] items = table.getSelection(); if (items.length == 0) return; editEntry(items[0]); } }); // Edit -> Copy subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("Copy")); subItem.setAccelerator(SWT.MOD1 + 'C'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { TableItem[] items = table.getSelection(); if (items.length == 0) return; copyBuffer = new String[table.getColumnCount()]; for (int i = 0; i < copyBuffer.length; i++) { copyBuffer[i] = items[0].getText(i); } } }); // Edit -> Paste subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("Paste")); subItem.setAccelerator(SWT.MOD1 + 'V'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { if (copyBuffer == null) return; TableItem item = new TableItem(table, SWT.NONE); item.setText(copyBuffer); isModified = true; } }); // Edit -> Delete subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("Delete")); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { TableItem[] items = table.getSelection(); if (items.length == 0) return; items[0].dispose(); isModified = true; } }); new MenuItem(menu, SWT.SEPARATOR); // Edit -> Sort(Cascade) subItem = new MenuItem(menu, SWT.CASCADE); subItem.setText(resAddressBook.getString("Sort")); Menu submenu = createSortMenu(); subItem.setMenu(submenu); return item; }
/** * Creates all the items located in the File submenu and associate all the menu items with their * appropriate functions. * * @param menuBar Menu the <code>Menu</code> that file contain the File submenu. */ private void createFileMenu(Menu menuBar) { // File menu. MenuItem item = new MenuItem(menuBar, SWT.CASCADE); item.setText(resAddressBook.getString("File_menu_title")); Menu menu = new Menu(shell, SWT.DROP_DOWN); item.setMenu(menu); /** Adds a listener to handle enabling and disabling some items in the Edit submenu. */ menu.addMenuListener( new MenuAdapter() { @Override public void menuShown(MenuEvent e) { Menu menu = (Menu) e.widget; MenuItem[] items = menu.getItems(); items[1].setEnabled(table.getSelectionCount() != 0); // edit contact items[5].setEnabled((file != null) && isModified); // save items[6].setEnabled(table.getItemCount() != 0); // save as } }); // File -> New Contact MenuItem subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("New_contact")); subItem.setAccelerator(SWT.MOD1 + 'N'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { newEntry(); } }); subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("Edit_contact")); subItem.setAccelerator(SWT.MOD1 + 'E'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { TableItem[] items = table.getSelection(); if (items.length == 0) return; editEntry(items[0]); } }); new MenuItem(menu, SWT.SEPARATOR); // File -> New Address Book subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("New_address_book")); subItem.setAccelerator(SWT.MOD1 + 'B'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { if (closeAddressBook()) { newAddressBook(); } } }); // File -> Open subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("Open_address_book")); subItem.setAccelerator(SWT.MOD1 + 'O'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { if (closeAddressBook()) { openAddressBook(); } } }); // File -> Save. subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("Save_address_book")); subItem.setAccelerator(SWT.MOD1 + 'S'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { save(); } }); // File -> Save As. subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("Save_book_as")); subItem.setAccelerator(SWT.MOD1 + 'A'); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { saveAs(); } }); new MenuItem(menu, SWT.SEPARATOR); // File -> Exit. subItem = new MenuItem(menu, SWT.NONE); subItem.setText(resAddressBook.getString("Exit")); subItem.addSelectionListener( new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { shell.close(); } }); }
public static void main(String[] args) { Shell shell = new Shell(); Display display = shell.getDisplay(); shell.setLayout(new FillLayout()); shell.setText("ExpandBar Example"); Menu menubar = new Menu(shell, SWT.BAR); shell.setMenuBar(menubar); MenuItem fileItem = new MenuItem(menubar, SWT.CASCADE); fileItem.setText("&File"); Menu submenu = new Menu(shell, SWT.DROP_DOWN); fileItem.setMenu(submenu); item = new MenuItem(submenu, SWT.PUSH); item.setText("New ExpandItem"); bar = new ExpandBar(shell, SWT.V_SCROLL); image = display.getSystemImage(SWT.ICON_QUESTION); // First item Composite composite = new Composite(bar, SWT.NONE); Menu popupmenu = new Menu(shell, SWT.POP_UP); MenuItem popupItem = new MenuItem(popupmenu, SWT.PUSH); popupItem.setText("Popup"); composite.setMenu(popupmenu); GridLayout 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); item1.setText("What is your favorite icon"); item1.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y); item1.setControl(composite); item1.setImage(image); // Second 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); Button button1 = new Button(composite, SWT.PUSH); button1.setText("Button 1"); Button button2 = new Button(composite, SWT.PUSH); button2.setText("Button 2"); ExpandItem item0 = new ExpandItem(bar, SWT.NONE); item0.setText("What is your favorite button"); item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y); item0.setControl(composite); item0.setImage(image); item0.setExpanded(false); item.addListener( SWT.Selection, new Listener() { @Override public void handleEvent(Event e) { ExpandItem item2 = new ExpandItem(bar, SWT.NONE); Composite composite = new Composite(bar, SWT.NONE); GridLayout layout = new GridLayout(2, false); composite.setLayout(layout); Label label = new Label(composite, SWT.NONE); label.setText("What is your name?"); Composite pcomposite = new Composite(composite, SWT.NONE); Text text = new Text(pcomposite, SWT.NONE); item2.setText("New Question"); text.pack(); composite.pack(); Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT); item2.setHeight(size.y); item2.setControl(composite); item2.setImage(image); item2.setExpanded(true); } }); bar.setSpacing(8); shell.setSize(400, 550); shell.open(); System.out.println("------------------------------"); System.out.println("getSize: " + button1.getSize()); System.out.println("getBounds: " + button1.getBounds()); System.out.println("computeSize: " + button1.computeSize(SWT.DEFAULT, SWT.DEFAULT)); System.out.println("getLocation: " + button1.getLocation()); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } image.dispose(); display.dispose(); }
/** Initializes the GUI. */ private void initGUI() { try { getShell() .addDisposeListener( new DisposeListener() { public void widgetDisposed(DisposeEvent evt) { shellWidgetDisposed(evt); } }); getShell() .addControlListener( new ControlAdapter() { public void controlResized(ControlEvent evt) { shellControlResized(evt); } }); getShell() .addControlListener( new ControlAdapter() { public void controlMoved(ControlEvent evt) { shellControlMoved(evt); } }); GridLayout thisLayout = new GridLayout(); this.setLayout(thisLayout); { GridData toolBarLData = new GridData(); toolBarLData.grabExcessHorizontalSpace = true; toolBarLData.horizontalAlignment = GridData.FILL; toolBar = new ToolBar(this, SWT.FLAT); toolBar.setLayoutData(toolBarLData); toolBar.setBackgroundImage(SWTResourceManager.getImage("images/ToolbarBackground.gif")); { newToolItem = new ToolItem(toolBar, SWT.NONE); newToolItem.setImage(SWTResourceManager.getImage("images/new.gif")); newToolItem.setToolTipText("New"); } { openToolItem = new ToolItem(toolBar, SWT.NONE); openToolItem.setToolTipText("Open"); openToolItem.setImage(SWTResourceManager.getImage("images/open.gif")); openToolItem.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent evt) { openToolItemWidgetSelected(evt); } }); } { saveToolItem = new ToolItem(toolBar, SWT.NONE); saveToolItem.setToolTipText("Save"); saveToolItem.setImage(SWTResourceManager.getImage("images/save.gif")); } } { clientArea = new Composite(this, SWT.NONE); GridData clientAreaLData = new GridData(); clientAreaLData.grabExcessHorizontalSpace = true; clientAreaLData.grabExcessVerticalSpace = true; clientAreaLData.horizontalAlignment = GridData.FILL; clientAreaLData.verticalAlignment = GridData.FILL; clientArea.setLayoutData(clientAreaLData); clientArea.setLayout(null); } { statusArea = new Composite(this, SWT.NONE); GridLayout statusAreaLayout = new GridLayout(); statusAreaLayout.makeColumnsEqualWidth = true; statusAreaLayout.horizontalSpacing = 0; statusAreaLayout.marginHeight = 0; statusAreaLayout.marginWidth = 0; statusAreaLayout.verticalSpacing = 0; statusAreaLayout.marginLeft = 3; statusAreaLayout.marginRight = 3; statusAreaLayout.marginTop = 3; statusAreaLayout.marginBottom = 3; statusArea.setLayout(statusAreaLayout); GridData statusAreaLData = new GridData(); statusAreaLData.horizontalAlignment = GridData.FILL; statusAreaLData.grabExcessHorizontalSpace = true; statusArea.setLayoutData(statusAreaLData); statusArea.setBackground(SWTResourceManager.getColor(239, 237, 224)); { statusText = new Label(statusArea, SWT.BORDER); statusText.setText(" Ready"); GridData txtStatusLData = new GridData(); txtStatusLData.horizontalAlignment = GridData.FILL; txtStatusLData.grabExcessHorizontalSpace = true; txtStatusLData.verticalIndent = 3; statusText.setLayoutData(txtStatusLData); } } thisLayout.verticalSpacing = 0; thisLayout.marginWidth = 0; thisLayout.marginHeight = 0; thisLayout.horizontalSpacing = 0; thisLayout.marginTop = 3; this.setSize(474, 312); { menu1 = new Menu(getShell(), SWT.BAR); getShell().setMenuBar(menu1); { fileMenuItem = new MenuItem(menu1, SWT.CASCADE); fileMenuItem.setText("&File"); { fileMenu = new Menu(fileMenuItem); { newFileMenuItem = new MenuItem(fileMenu, SWT.PUSH); newFileMenuItem.setText("&New"); newFileMenuItem.setImage(SWTResourceManager.getImage("images/new.gif")); } { openFileMenuItem = new MenuItem(fileMenu, SWT.PUSH); openFileMenuItem.setText("&Open"); openFileMenuItem.setImage(SWTResourceManager.getImage("images/open.gif")); openFileMenuItem.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent evt) { openFileMenuItemWidgetSelected(evt); } }); } { closeFileMenuItem = new MenuItem(fileMenu, SWT.CASCADE); closeFileMenuItem.setText("Close"); } { fileMenuSep1 = new MenuItem(fileMenu, SWT.SEPARATOR); } { saveFileMenuItem = new MenuItem(fileMenu, SWT.PUSH); saveFileMenuItem.setText("&Save"); saveFileMenuItem.setImage(SWTResourceManager.getImage("images/save.gif")); saveFileMenuItem.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent evt) { saveFileMenuItemWidgetSelected(evt); } }); } { fileMenuSep2 = new MenuItem(fileMenu, SWT.SEPARATOR); } { exitMenuItem = new MenuItem(fileMenu, SWT.CASCADE); exitMenuItem.setText("E&xit"); exitMenuItem.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent evt) { exitMenuItemWidgetSelected(evt); } }); } fileMenuItem.setMenu(fileMenu); } } { helpMenuItem = new MenuItem(menu1, SWT.CASCADE); helpMenuItem.setText("&Help"); { helpMenu = new Menu(helpMenuItem); { aboutMenuItem = new MenuItem(helpMenu, SWT.CASCADE); aboutMenuItem.setText("&About"); aboutMenuItem.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent evt) { aboutMenuItemWidgetSelected(evt); } }); } helpMenuItem.setMenu(helpMenu); } } } } catch (Exception e) { e.printStackTrace(); } }