public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); GridLayout gridLayout = new GridLayout(); gridLayout.numColumns = 3; shell.setLayout(gridLayout); ToolBar toolbar = new ToolBar(shell, SWT.NONE); ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH); itemBack.setText("Back"); ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH); itemForward.setText("Forward"); ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH); itemStop.setText("Stop"); ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH); itemRefresh.setText("Refresh"); ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH); itemGo.setText("Go"); GridData data = new GridData(); data.horizontalSpan = 3; toolbar.setLayoutData(data); Label labelAddress = new Label(shell, SWT.NONE); labelAddress.setText("Address"); final Text location = new Text(shell, SWT.BORDER); data = new GridData(); data.horizontalAlignment = GridData.FILL; data.horizontalSpan = 2; data.grabExcessHorizontalSpace = true; location.setLayoutData(data); final Browser browser; try { browser = new Browser(shell, SWT.NONE); } catch (SWTError e) { System.out.println("Could not instantiate Browser: " + e.getMessage()); display.dispose(); return; } data = new GridData(); data.horizontalAlignment = GridData.FILL; data.verticalAlignment = GridData.FILL; data.horizontalSpan = 3; data.grabExcessHorizontalSpace = true; data.grabExcessVerticalSpace = true; browser.setLayoutData(data); final Label status = new Label(shell, SWT.NONE); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 2; status.setLayoutData(data); final ProgressBar progressBar = new ProgressBar(shell, SWT.NONE); data = new GridData(); data.horizontalAlignment = GridData.END; progressBar.setLayoutData(data); /* event handling */ Listener listener = new Listener() { @Override public void handleEvent(Event event) { ToolItem item = (ToolItem) event.widget; String string = item.getText(); if (string.equals("Back")) browser.back(); else if (string.equals("Forward")) browser.forward(); else if (string.equals("Stop")) browser.stop(); else if (string.equals("Refresh")) browser.refresh(); else if (string.equals("Go")) browser.setUrl(location.getText()); } }; browser.addProgressListener( new ProgressListener() { @Override public void changed(ProgressEvent event) { if (event.total == 0) return; int ratio = event.current * 100 / event.total; progressBar.setSelection(ratio); } @Override public void completed(ProgressEvent event) { progressBar.setSelection(0); } }); browser.addStatusTextListener( new StatusTextListener() { @Override public void changed(StatusTextEvent event) { status.setText(event.text); } }); browser.addLocationListener( new LocationListener() { @Override public void changed(LocationEvent event) { if (event.top) location.setText(event.location); } @Override public void changing(LocationEvent event) {} }); itemBack.addListener(SWT.Selection, listener); itemForward.addListener(SWT.Selection, listener); itemStop.addListener(SWT.Selection, listener); itemRefresh.addListener(SWT.Selection, listener); itemGo.addListener(SWT.Selection, listener); location.addListener( SWT.DefaultSelection, new Listener() { @Override public void handleEvent(Event e) { browser.setUrl(location.getText()); } }); shell.open(); browser.setUrl("http://eclipse.org"); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } 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(); } }