Example usage for org.eclipse.swt.widgets Button Button

List of usage examples for org.eclipse.swt.widgets Button Button

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Button Button.

Prototype


public Button(Composite parent, int style) 

Source Link

Document

Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

Usage

From source file:TabFolderExample.java

public TabFolderExample() {
    shell.setLayout(new FillLayout());

    final TabFolder tabFolder = new TabFolder(shell, SWT.BOTTOM);

    Button button = new Button(tabFolder, SWT.NULL);
    button.setText("This is a button.");

    TabItem tabItem1 = new TabItem(tabFolder, SWT.NULL);
    tabItem1.setText("item #1");
    tabItem1.setImage(icon);//from   ww  w . jav a2s .  co m
    tabItem1.setControl(button);

    Text text = new Text(tabFolder, SWT.MULTI);
    text.setText("This is a text control.");

    TabItem tabItem2 = new TabItem(tabFolder, SWT.NULL);
    tabItem2.setText("item #2");
    tabItem2.setImage(icon);
    tabItem2.setControl(text);

    Label label = new Label(tabFolder, SWT.NULL);
    label.setText("This is a text lable.");

    TabItem tabItem3 = new TabItem(tabFolder, SWT.NULL);
    tabItem3.setText("item #3");
    tabItem3.setControl(label);

    tabFolder.addSelectionListener(new SelectionListener() {
        public void widgetSelected(SelectionEvent e) {
            System.out.println("Selected item index = " + tabFolder.getSelectionIndex());
            System.out.println("Selected item = "
                    + (tabFolder.getSelection() == null ? "null" : tabFolder.getSelection()[0].toString()));
        }

        public void widgetDefaultSelected(SelectionEvent e) {
            widgetSelected(e);
        }
    });

    //tabFolder.setSelection(new TabItem[]{tabItem2, tabItem3});
    //tabFolder.setSelection(2);

    shell.setSize(400, 120);
    shell.open();
    //textUser.forceFocus();

    System.out.println(tabFolder.getSelectionIndex());

    // Set up the event loop.
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();
}

From source file:Spinner.java

public Spinner(Composite parent, int style) {

    super(parent, style);

    text = new Text(this, style | SWT.SINGLE | SWT.BORDER);

    up = new Button(this, style | SWT.ARROW | SWT.UP);

    down = new Button(this, style | SWT.ARROW | SWT.DOWN);

    text.addListener(SWT.Verify, new Listener() {

        public void handleEvent(Event e) {

            verify(e);/*  w  w w.j ava2s  .  co  m*/

        }

    });

    text.addListener(SWT.Traverse, new Listener() {

        public void handleEvent(Event e) {

            traverse(e);

        }

    });

    up.addListener(SWT.Selection, new Listener() {

        public void handleEvent(Event e) {

            up();

        }

    });

    down.addListener(SWT.Selection, new Listener() {

        public void handleEvent(Event e) {

            down();

        }

    });

    addListener(SWT.Resize, new Listener() {

        public void handleEvent(Event e) {

            resize();

        }

    });

    addListener(SWT.FocusIn, new Listener() {

        public void handleEvent(Event e) {

            focusIn();

        }

    });

    text.setFont(getFont());

    minimum = 0;

    maximum = 9;

    setSelection(minimum);

}

From source file:org.eclipse.swt.examples.controlexample.ScrollableTab.java

/**
 * Creates the "Style" group.//from ww w.  j  av  a2s. c  om
 */
@Override
void createStyleGroup() {
    super.createStyleGroup();

    /* Create the extra widgets */
    singleButton = new Button(styleGroup, SWT.RADIO);
    singleButton.setText("SWT.SINGLE");
    multiButton = new Button(styleGroup, SWT.RADIO);
    multiButton.setText("SWT.MULTI");
    horizontalButton = new Button(styleGroup, SWT.CHECK);
    horizontalButton.setText("SWT.H_SCROLL");
    horizontalButton.setSelection(true);
    verticalButton = new Button(styleGroup, SWT.CHECK);
    verticalButton.setText("SWT.V_SCROLL");
    verticalButton.setSelection(true);
    borderButton = new Button(styleGroup, SWT.CHECK);
    borderButton.setText("SWT.BORDER");
}

From source file:CountNumbers.java

public CountNumbers() {
    GridLayout gridLayout = new GridLayout(1, true);
    shell.setLayout(gridLayout);/*  w w w  . j av  a  2  s  .  co  m*/

    button = new Button(shell, SWT.BORDER);
    button.setText("Start to count");

    progressBar = new ProgressBar(shell, SWT.SMOOTH);
    progressBar.setMinimum(0);
    progressBar.setMaximum(10);

    final Thread countThread = new Thread() {
        public void run() {
            for (int i = 0; i <= 10; i++) {
                final int num = i;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                shell.getDisplay().asyncExec(new Runnable() {
                    public void run() {
                        if (button.isDisposed() || progressBar.isDisposed())
                            return;
                        button.setText("Counting: " + num);
                        progressBar.setSelection(num);
                        //progressBar.redraw();
                    }
                });
            }
        }
    };

    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            button.setEnabled(false);
            countThread.start();
        }
    });

    progressBar.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
            System.out.println("PAINT");
            // string to draw. 
            String string = (progressBar.getSelection() * 1.0
                    / (progressBar.getMaximum() - progressBar.getMinimum()) * 100) + "%";

            Point point = progressBar.getSize();
            Font font = new Font(shell.getDisplay(), "Courier", 10, SWT.BOLD);
            e.gc.setFont(font);
            e.gc.setForeground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));

            FontMetrics fontMetrics = e.gc.getFontMetrics();
            int stringWidth = fontMetrics.getAverageCharWidth() * string.length();
            int stringHeight = fontMetrics.getHeight();

            e.gc.drawString(string, (point.x - stringWidth) / 2, (point.y - stringHeight) / 2, true);
            font.dispose();
        }
    });

    button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    progressBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    shell.setSize(300, 100);
    shell.open();
    //textUser.forceFocus();

    // Set up the event loop.
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();
}

From source file:ShowMyTitleAreaDialog.java

/**
 * Creates the main window's contents/*from w  w w  .  j  ava  2  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(1, true));

    // Create the button
    Button show = new Button(composite, SWT.NONE);
    show.setText("Show");

    final Shell shell = parent.getShell();

    // Display the TitleAreaDialog
    show.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            // Create and show the dialog
            MyTitleAreaDialog dlg = new MyTitleAreaDialog(shell);
            dlg.open();
        }
    });

    parent.pack();
    return composite;
}

From source file:Ch11WizardComposite.java

protected void buildControls() {
    final Composite parent = this;
    FillLayout layout = new FillLayout();
    parent.setLayout(layout);// ww  w  .  java  2s .  c om

    Button dialogBtn = new Button(parent, SWT.PUSH);
    dialogBtn.setText("Wizard Dialog...");
    dialogBtn.addSelectionListener(new SelectionListener() {

        public void widgetSelected(SelectionEvent e) {
            WizardDialog dialog = new WizardDialog(parent.getShell(), new ProjectWizard());
            dialog.open();
        }

        public void widgetDefaultSelected(SelectionEvent e) {
        }
    });
}

From source file:MainClass.java

protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(1, true));

    final Button indeterminate = new Button(composite, SWT.CHECK);
    indeterminate.setText("Indeterminate");
    Button showProgress = new Button(composite, SWT.NONE);
    showProgress.setText("Show Progress");

    final Shell shell = parent.getShell();

    showProgress.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            try {
                new ProgressMonitorDialog(shell).run(true, true,
                        new LongRunningOperation(indeterminate.getSelection()));
            } catch (InvocationTargetException e) {
                MessageDialog.openError(shell, "Error", e.getMessage());
            } catch (InterruptedException e) {
                MessageDialog.openInformation(shell, "Cancelled", e.getMessage());
            }/* w  w w.  j a  va2  s  . c o  m*/
        }
    });

    parent.pack();
    return composite;
}

From source file:DirFileSelection.java

public DirFileSelection() {
    label = new Label(shell, SWT.BORDER | SWT.WRAP);
    label.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
    label.setText("Select a dir/file by clicking the buttons below.");

    buttonSelectDir = new Button(shell, SWT.PUSH);
    buttonSelectDir.setText("Select a directory");
    buttonSelectDir.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            DirectoryDialog directoryDialog = new DirectoryDialog(shell);

            directoryDialog.setFilterPath(selectedDir);
            directoryDialog.setMessage("Please select a directory and click OK");

            String dir = directoryDialog.open();
            if (dir != null) {
                label.setText("Selected dir: " + dir);
                selectedDir = dir;// w w w .j  ava  2s .  co  m
            }
        }
    });

    buttonSelectFile = new Button(shell, SWT.PUSH);
    buttonSelectFile.setText("Select a file/multiple files");
    buttonSelectFile.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            FileDialog fileDialog = new FileDialog(shell, SWT.MULTI);

            fileDialog.setFilterPath(fileFilterPath);

            fileDialog.setFilterExtensions(new String[] { "*.rtf", "*.html", "*.*" });
            fileDialog.setFilterNames(new String[] { "Rich Text Format", "HTML Document", "Any" });

            String firstFile = fileDialog.open();

            if (firstFile != null) {
                fileFilterPath = fileDialog.getFilterPath();
                String[] selectedFiles = fileDialog.getFileNames();
                StringBuffer sb = new StringBuffer(
                        "Selected files under dir " + fileDialog.getFilterPath() + ": \n");
                for (int i = 0; i < selectedFiles.length; i++) {
                    sb.append(selectedFiles[i] + "\n");
                }
                label.setText(sb.toString());
            }
        }
    });

    label.setBounds(0, 0, 400, 60);
    buttonSelectDir.setBounds(0, 65, 200, 30);
    buttonSelectFile.setBounds(200, 65, 200, 30);

    shell.pack();
    shell.open();
    //textUser.forceFocus();

    // Set up the event loop.
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
        }
    }

    display.dispose();
}

From source file:BusyIndicatorTest.java

/**
 * Create the window's contents/*from   ww w  . j a v a  2  s  . c om*/
 * 
 * @param shell the parent shell
 */
private void createContents(Shell shell) {
    shell.setLayout(new FillLayout());
    final Button button = new Button(shell, SWT.PUSH);
    button.setText(RUN);
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            // Change the button's text
            button.setText(IS_RUNNING);

            // Show the busy indicator
            BusyIndicator.showWhile(button.getDisplay(), new SleepThread(SLEEP_TIME));

            // Thread has completed; reset the button's text
            button.setText(RUN);
        }
    });
}

From source file:PopupListTest.java

/**
 * Creates the main window's contents//from  w ww  .j a v a  2  s. com
 * 
 * @param shell the main window
 */
private void createContents(final Shell shell) {
    shell.setLayout(new RowLayout());

    // Create a button to launch the list
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Push Me");
    button.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            // Create a list
            PopupList list = new PopupList(shell);

            // Add the items to the list
            list.setItems(OPTIONS);

            // Open the list and get the selected item
            String selected = list.open(shell.getBounds());

            // Print the item to the console
            System.out.println(selected);
        }
    });
}