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

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

Introduction

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

Prototype

public boolean setFocus() 

Source Link

Document

Causes the receiver to have the keyboard focus, such that all keyboard events will be delivered to it.

Usage

From source file:TreeEditorButton.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Text Tree Editor");
    shell.setLayout(new FillLayout());

    final Tree tree = new Tree(shell, SWT.SINGLE);
    for (int i = 0; i < 3; i++) {
        TreeItem iItem = new TreeItem(tree, SWT.NONE);
        iItem.setText("Item " + (i + 1));
        for (int j = 0; j < 3; j++) {
            TreeItem jItem = new TreeItem(iItem, SWT.NONE);
            jItem.setText("Sub Item " + (j + 1));
            for (int k = 0; k < 3; k++) {
                new TreeItem(jItem, SWT.NONE).setText("Sub Sub Item " + (k + 1));
            }/*from w  ww. jav  a 2s.c  o m*/
            jItem.setExpanded(true);
        }
        iItem.setExpanded(true);
    }

    final TreeEditor editor = new TreeEditor(tree);
    editor.horizontalAlignment = SWT.LEFT;
    editor.grabHorizontal = true;

    tree.addMouseListener(new MouseAdapter() {
        public void mouseDown(MouseEvent event) {
            final TreeItem item = tree.getSelection()[0];

            final Button bn = new Button(tree, SWT.NONE);
            bn.setText("click");
            bn.setFocus();

            bn.addSelectionListener(new SelectionListener() {

                public void widgetSelected(SelectionEvent arg0) {
                    int style = SWT.ICON_QUESTION | SWT.YES | SWT.NO;

                    MessageBox messageBox = new MessageBox(shell, style);
                    messageBox.setMessage("Message");
                    int rc = messageBox.open();

                    item.setText(rc + "");
                    bn.dispose();
                }

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

            editor.setEditor(bn, item);
        }
    });

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

From source file:widgetTest3.java

/** * StackLayout ** */

public static void createStackLayout(Composite composite) {
    //       Neues Composite erzeugen
    final Composite stackComposite = new Composite(composite, SWT.NULL);
    final StackLayout stackLayout = new StackLayout();
    //       Text-Buttons erzeugen
    final Button buttonA = new Button(stackComposite, SWT.PUSH);
    buttonA.setText("Taste A");
    final Button buttonB = new Button(stackComposite, SWT.PUSH);
    buttonB.setText("Taste B");
    //       Auf Klickereignisse reagieren
    buttonA.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            stackLayout.topControl = buttonB;
            //       Neues Layout erzwingen
            stackComposite.layout();//from  w w  w .ja  va2 s .c o  m
            //       Fokus auf sichtbare Taste setzen
            buttonB.setFocus();
        }
    });
    buttonB.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            stackLayout.topControl = buttonA;
            //       Neues Layout erzwingen
            stackComposite.layout();
            //       Fokus auf sichtbare Taste setzen
            buttonA.setFocus();
        }
    });
    //       Layout initialisieren
    stackLayout.topControl = buttonA;
    stackLayout.marginWidth = 10;
    stackLayout.marginHeight = 5;
    //       Layout setzen
    stackComposite.setLayout(stackLayout);
}

From source file:BugTracker.java

public BugTracker() {
    GridLayout gridLayout = new GridLayout();
    shell.setLayout(gridLayout);/*from   w ww .  java 2s . co m*/
    shell.setText("Bug Tracking System");

    // Action.
    Action actionAddNew = new Action("New bug") {
        public void run() {
            // Append.
            TableItem item = new TableItem(table, SWT.NULL);
            item.setImage(bugIcon);
            table.select(table.getItemCount() - 1);
        }
    };

    Action actionDelete = new Action("Delete selected") {
        public void run() {
            int index = table.getSelectionIndex();
            if (index < 0) {
                System.out.println("Please select an item first. ");
                return;
            }
            MessageBox messageBox = new MessageBox(shell, SWT.YES | SWT.NO);
            messageBox.setText("Confirmation");
            messageBox.setMessage("Are you sure to remove the bug with id #" + table.getItem(index).getText(0));
            if (messageBox.open() == SWT.YES) {
                table.remove(index);
            }
        }
    };

    Action actionSave = new Action("Save") {
        public void run() {
            saveBugs();
        }
    };

    ToolBar toolBar = new ToolBar(shell, SWT.RIGHT | SWT.FLAT);

    ToolBarManager manager = new ToolBarManager(toolBar);
    manager.add(actionAddNew);
    manager.add(actionDelete);
    manager.add(new Separator());
    manager.add(actionSave);
    manager.update(true);

    table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
    table.setLayoutData(new GridData(GridData.FILL_BOTH));

    table.setLinesVisible(true);
    table.setHeaderVisible(true);

    TableColumn tcID = new TableColumn(table, SWT.LEFT);
    tcID.setText("ID");

    TableColumn tcSummary = new TableColumn(table, SWT.NULL);
    tcSummary.setText("Summary");

    TableColumn tcAssignedTo = new TableColumn(table, SWT.NULL);
    tcAssignedTo.setText("Assigned to");

    TableColumn tcSolved = new TableColumn(table, SWT.NULL);
    tcSolved.setText("Solved");

    tcID.setWidth(60);
    tcSummary.setWidth(200);
    tcAssignedTo.setWidth(80);
    tcSolved.setWidth(50);

    table.pack();

    // Table editor.
    final TableEditor editor = new TableEditor(table);

    table.addListener(SWT.MouseDown, new Listener() {
        public void handleEvent(Event event) {
            // Locate the cell position.
            Point point = new Point(event.x, event.y);
            final TableItem item = table.getItem(point);
            if (item == null)
                return;
            int column = -1;
            for (int i = 0; i < table.getColumnCount(); i++) {
                Rectangle rect = item.getBounds(i);
                if (rect.contains(point))
                    column = i;
            }
            if (column != 3)
                return;

            // Cell position located, now open the table editor.

            final Button button = new Button(table, SWT.CHECK);
            button.setSelection(item.getText(column).equalsIgnoreCase("YES"));

            editor.horizontalAlignment = SWT.LEFT;
            editor.grabHorizontal = true;

            editor.setEditor(button, item, column);

            final int selectedColumn = column;
            Listener buttonListener = new Listener() {
                public void handleEvent(final Event e) {
                    switch (e.type) {
                    case SWT.FocusOut:
                        item.setText(selectedColumn, button.getSelection() ? "YES" : "NO");
                        button.dispose();
                        break;
                    case SWT.Traverse:
                        switch (e.detail) {
                        case SWT.TRAVERSE_RETURN:
                            item.setText(selectedColumn, button.getSelection() ? "YES" : "NO");
                            //FALL THROUGH
                        case SWT.TRAVERSE_ESCAPE:
                            button.dispose();
                            e.doit = false;
                        }
                        break;
                    }
                }
            };

            button.addListener(SWT.FocusOut, buttonListener);
            button.addListener(SWT.Traverse, buttonListener);

            button.setFocus();

        }
    });

    table.addListener(SWT.MouseDown, new Listener() {
        public void handleEvent(Event event) {
            // Locate the cell position.
            Point point = new Point(event.x, event.y);
            final TableItem item = table.getItem(point);
            if (item == null)
                return;
            int column = -1;
            for (int i = 0; i < table.getColumnCount(); i++) {
                Rectangle rect = item.getBounds(i);
                if (rect.contains(point))
                    column = i;
            }
            if (column < 0 || column >= 3)
                return;

            // Cell position located, now open the table editor.

            final Text text = new Text(table, SWT.NONE);
            text.setText(item.getText(column));

            editor.horizontalAlignment = SWT.LEFT;
            editor.grabHorizontal = true;

            editor.setEditor(text, item, column);

            final int selectedColumn = column;
            Listener textListener = new Listener() {
                public void handleEvent(final Event e) {
                    switch (e.type) {
                    case SWT.FocusOut:
                        item.setText(selectedColumn, text.getText());
                        text.dispose();
                        break;
                    case SWT.Traverse:
                        switch (e.detail) {
                        case SWT.TRAVERSE_RETURN:
                            item.setText(selectedColumn, text.getText());
                            //FALL THROUGH
                        case SWT.TRAVERSE_ESCAPE:
                            text.dispose();
                            e.doit = false;
                        }
                        break;
                    }
                }
            };

            text.addListener(SWT.FocusOut, textListener);
            text.addListener(SWT.Traverse, textListener);

            text.setFocus();
        }

    });

    loadBugs();

    table.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            System.out.println("Selected: " + table.getSelection()[0]);
        }
    });

    Listener sortListener = new Listener() {
        public void handleEvent(Event event) {
            if (!(event.widget instanceof TableColumn))
                return;
            TableColumn tc = (TableColumn) event.widget;
            sortTable(table, table.indexOf(tc));
            System.out.println("The table is sorted by column #" + table.indexOf(tc));
        }
    };

    for (int i = 0; i < table.getColumnCount(); i++)
        ((TableColumn) table.getColumn(i)).addListener(SWT.Selection, sortListener);

    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:PrintKTableExample.java

/**
 * @param parent/*  ww w.  j a v a  2 s  .  co  m*/
 * @param title
 * @param icon
 */
public PrintPreview(Shell parent, String title, Image icon, PDocument doc) {
    super(parent, title, icon);
    createContents();
    document = doc;
    page = 1;
    percent = 100;
    layoutNeccessary = true;

    addToolItem("print", "Drucken ...", IconSource.getImage("print"));
    addToolItem("first", "erste Seite", IconSource.getImage("i2"));
    addToolItem("prev", "vorherige Seite", IconSource.getImage("i3"));
    addToolItem("next", "nachste Seite", IconSource.getImage("i4"));
    addToolItem("last", "letzte Seite", IconSource.getImage("i5"));
    Button close = addButtonRight("&SchlieBen", "");
    // addButtonRight("Seite &einrichten","");
    close.setFocus();

    guiShell.addShellListener(new ShellAdapter() {
        public void shellClosed(ShellEvent arg0) {
            onClose();
        }
    });

    Composite comp = new Composite(guiToolBarArea, SWT.BORDER);
    comp.setLayout(new FillLayout());
    guiPageLabel = new CLabel(comp, SWT.NONE);
    guiPageLabel.setText(guiPageLabel.getText() + "        ");
    guiPageLabel.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
    adjustToToolBar(comp);

    guiZoom = new Combo(guiToolBarArea, SWT.BORDER | SWT.READ_ONLY);
    guiZoom.add("500%");
    guiZoom.add("200%");
    guiZoom.add("100%");
    guiZoom.add("80%");
    guiZoom.add("50%");
    guiZoom.add("20%");
    guiZoom.add("passend");
    adjustToToolBar(guiZoom);
    guiZoom.setToolTipText("VorschaugroBe");
    guiZoom.select(2);
    guiZoom.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent arg0) {
            onCombo(((Combo) arg0.widget).getText());
        }
    });
    guiMainArea.setLayout(new FillLayout());
    guiScrollArea = new ScrolledComposite(guiMainArea, SWT.H_SCROLL | SWT.V_SCROLL);
    guiImageLabel = new Label(guiScrollArea, SWT.NONE);
    guiScrollArea.setContent(guiImageLabel);
    if (guiImageLabel.getImage() != null)
        guiImageLabel.getImage().dispose();
    guiImageLabel.setImage(getPageImage(page));
    guiPageLabel.setText(" Seite " + page + " von " + document.getNumOfPages() + "       ");
    guiImageLabel.setSize(guiImageLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT));

}