Example usage for java.awt Component requestFocus

List of usage examples for java.awt Component requestFocus

Introduction

In this page you can find the example usage for java.awt Component requestFocus.

Prototype

public void requestFocus() 

Source Link

Document

Requests that this Component get the input focus, and that this Component's top-level ancestor become the focused Window.

Usage

From source file:Main.java

public static Component compositeRequestFocus(Component component) {
    if (component instanceof Container) {
        Container container = (Container) component;
        if (container.isFocusCycleRoot()) {
            FocusTraversalPolicy policy = container.getFocusTraversalPolicy();
            Component comp = policy.getDefaultComponent(container);
            if (comp != null) {
                comp.requestFocus();
                return comp;
            }/*from   ww  w.j a v a2  s .com*/
        }
        Container rootAncestor = container.getFocusCycleRootAncestor();
        if (rootAncestor != null) {
            FocusTraversalPolicy policy = rootAncestor.getFocusTraversalPolicy();
            Component comp = policy.getComponentAfter(rootAncestor, container);

            if (comp != null && SwingUtilities.isDescendingFrom(comp, container)) {
                comp.requestFocus();
                return comp;
            }
        }
    }
    if (component.isFocusable()) {
        component.requestFocus();
        return component;
    }
    return null;
}

From source file:eu.delving.sip.base.VisualFeedback.java

private static void acquireFocus() {
    Component fo = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    if (fo != null && fo.isShowing())
        fo.requestFocus();
}

From source file:com.aw.swing.mvp.focus.ConcurrentFocusManager.java

public void requestFocus(String description, Component target) {
    target.requestFocus();
}

From source file:SwingUtil.java

/**
 * Get a file selection using the FileChooser dialog.
 *
 * @param owner/*  ww w .  ja  v a2 s .  c om*/
 * The parent of this modal dialog.
 * @param defaultSelection
 * The default file selection as a file.
 * @param filter
 * An extension filter
 * @param title
 * The caption for the dialog.
 *
 * @return
 * A selected file or null if no selection is made.
 */
public static File getFileChoice(Component owner, File defaultSelection, FileFilter filter, String title) {
    //
    // There is apparently a bug in the native Windows FileSystem class that
    // occurs when you use a file chooser and there is a security manager
    // active. An error dialog is displayed indicating there is no disk in
    // Drive A:. To avoid this, the security manager is temporarily set to
    // null and then reset after the file chooser is closed.
    //
    SecurityManager sm = null;
    File choice = null;
    JFileChooser chooser = null;

    sm = System.getSecurityManager();
    System.setSecurityManager(null);

    chooser = new JFileChooser();
    if (defaultSelection.isDirectory()) {
        chooser.setCurrentDirectory(defaultSelection);
    } else {
        chooser.setSelectedFile(defaultSelection);
    }
    chooser.setFileFilter(filter);
    chooser.setDialogTitle(title);
    chooser.setApproveButtonText("OK");
    int v = chooser.showOpenDialog(owner);

    owner.requestFocus();
    switch (v) {
    case JFileChooser.APPROVE_OPTION:
        if (chooser.getSelectedFile() != null) {
            choice = chooser.getSelectedFile();
        }
        break;
    case JFileChooser.CANCEL_OPTION:
    case JFileChooser.ERROR_OPTION:
    }
    chooser.removeAll();
    chooser = null;
    System.setSecurityManager(sm);
    return choice;
}

From source file:SwingUtil.java

/**
 * Open a JFileChooser dialog for selecting a directory and return the
 * selected directory./*from ww w . j a  va2 s  . co  m*/
 *
 *
 * @param owner
 * The frame or dialog that controls the invokation of this dialog.
 * @param defaultDir
 * The directory to show when the dialog opens.
 * @param title
 * Tile for the dialog.
 *
 * @return
 * The selected directory as a File. Null if user cancels dialog without
 * a selection.
 *
 */
public static File getDirectoryChoice(Component owner, File defaultDir, String title) {
    //
    // There is apparently a bug in the native Windows FileSystem class that
    // occurs when you use a file chooser and there is a security manager
    // active. An error dialog is displayed indicating there is no disk in
    // Drive A:. To avoid this, the security manager is temporarily set to
    // null and then reset after the file chooser is closed.
    //
    SecurityManager sm = null;
    JFileChooser chooser = null;
    File choice = null;

    sm = System.getSecurityManager();
    System.setSecurityManager(null);
    chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    if ((defaultDir != null) && defaultDir.exists() && defaultDir.isDirectory()) {
        chooser.setCurrentDirectory(defaultDir);
        chooser.setSelectedFile(defaultDir);
    }
    chooser.setDialogTitle(title);
    chooser.setApproveButtonText("OK");
    int v = chooser.showOpenDialog(owner);

    owner.requestFocus();
    switch (v) {
    case JFileChooser.APPROVE_OPTION:
        if (chooser.getSelectedFile() != null) {
            if (chooser.getSelectedFile().exists()) {
                choice = chooser.getSelectedFile();
            } else {
                File parentFile = new File(chooser.getSelectedFile().getParent());

                choice = parentFile;
            }
        }
        break;
    case JFileChooser.CANCEL_OPTION:
    case JFileChooser.ERROR_OPTION:
    }
    chooser.removeAll();
    chooser = null;
    System.setSecurityManager(sm);
    return choice;
}

From source file:self.philbrown.javaQuery.$.java

/**
 * Gives focus to the first focusable view in the current selection.
 * @return this/*from  ww w  .  j a  v  a 2  s. c  om*/
 */
public $ focus() {
    for (Component view : this.views) {
        view.requestFocus();
        if (view.hasFocus()) {
            break;
        }
    }
    return this;
}

From source file:org.kontalk.view.ThreadDetails.java

ThreadDetails(final Component focusGainer, KonThread thread) {
    mThread = thread;//from  w ww  .  j  av a 2  s.  co m

    GroupPanel groupPanel = new GroupPanel(View.GAP_BIG, false);
    groupPanel.setMargin(View.MARGIN_BIG);

    groupPanel.add(new WebLabel(Tr.tr("Edit Chat")).setBoldFont());
    groupPanel.add(new WebSeparator(true, true));

    // editable fields
    groupPanel.add(new WebLabel(Tr.tr("Subject:")));
    String subj = mThread.getSubject();
    mSubjectField = new WebTextField(subj, 22);
    mSubjectField.setInputPrompt(subj);
    mSubjectField.setHideInputPromptOnFocus(false);
    groupPanel.add(mSubjectField);
    groupPanel.add(new WebSeparator(true, true));

    final WebSlider colorSlider = new WebSlider(WebSlider.HORIZONTAL);

    groupPanel.add(new WebLabel(Tr.tr("Custom Background")));
    mColorOpt = new WebRadioButton(Tr.tr("Color:") + " ");
    Optional<Color> optBGColor = mThread.getViewSettings().getBGColor();
    mColorOpt.setSelected(optBGColor.isPresent());
    mColorOpt.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            colorSlider.setEnabled(e.getStateChange() == ItemEvent.SELECTED);
        }
    });
    mColor = new WebButton();
    mColor.setMinimumHeight(25);
    Color oldColor = optBGColor.orElse(DEFAULT_BG);
    mColor.setBottomBgColor(oldColor);
    groupPanel.add(new GroupPanel(GroupingType.fillLast, mColorOpt, mColor));

    colorSlider.setMinimum(0);
    colorSlider.setMaximum(100);
    colorSlider.setPaintTicks(false);
    colorSlider.setPaintLabels(false);
    colorSlider.setEnabled(optBGColor.isPresent());
    final GradientData gradientData = GradientData.getDefaultValue();
    // TODO set location for color
    gradientData.getColor(0);
    colorSlider.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            float v = colorSlider.getValue() / (float) 100;
            Color c = gradientData.getColorForLocation(v);
            mColor.setBottomBgColor(c);
            mColor.repaint();
        }
    });
    groupPanel.add(colorSlider);

    mImgOpt = new WebRadioButton(Tr.tr("Image:") + " ");
    String imgPath = mThread.getViewSettings().getImagePath();
    mImgOpt.setSelected(!imgPath.isEmpty());
    mImgOpt.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            mImgChooser.setEnabled(e.getStateChange() == ItemEvent.SELECTED);
            mImgChooser.getChooseButton().setEnabled(e.getStateChange() == ItemEvent.SELECTED);
        }
    });
    mImgChooser = Utils.createImageChooser(!imgPath.isEmpty(), imgPath);
    groupPanel.add(new GroupPanel(GroupingType.fillLast, mImgOpt, mImgChooser));
    UnselectableButtonGroup.group(mColorOpt, mImgOpt);
    groupPanel.add(new WebSeparator());

    //        groupPanel.add(new WebLabel(Tr.tr("Participants:")));
    //        mParticipantsList = new WebCheckBoxList();
    //        mParticipantsList.setVisibleRowCount(10);
    //        for (User oneUser : UserList.getInstance().getAll()) {
    //            boolean selected = mThread.getUser().contains(oneUser);
    //            mParticipantsList.getCheckBoxListModel().addCheckBoxElement(
    //                    new UserElement(oneUser),
    //                    selected);
    //        }
    final WebButton saveButton = new WebButton(Tr.tr("Save"));
    //        mParticipantsList.getModel().addListDataListener(new ListDataListener() {
    //            @Override
    //            public void intervalAdded(ListDataEvent e) {
    //            }
    //            @Override
    //            public void intervalRemoved(ListDataEvent e) {
    //            }
    //            @Override
    //            public void contentsChanged(ListDataEvent e) {
    //                saveButton.setEnabled(!mParticipantsList.getCheckedValues().isEmpty());
    //            }
    //        });
    //
    //        groupPanel.add(new WebScrollPane(mParticipantsList));
    //        groupPanel.add(new WebSeparator(true, true));

    this.add(groupPanel, BorderLayout.CENTER);

    saveButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            //                if (mParticipantsList.getCheckedValues().size() > 1) {
            //                        String infoText = Tr.t/r("More than one receiver not supported (yet).");
            //                        WebOptionPane.showMessageDialog(ThreadListView.this,
            //                                infoText,
            //                                Tr.t/r("Sorry"),
            //                                WebOptionPane.INFORMATION_MESSAGE);
            //                    return;
            //                }
            ThreadDetails.this.save();

            // close popup
            focusGainer.requestFocus();
        }
    });
    //this.getRootPane().setDefaultButton(saveButton);

    GroupPanel buttonPanel = new GroupPanel(2, saveButton);
    buttonPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
    this.add(buttonPanel, BorderLayout.SOUTH);
}

From source file:edu.ku.brc.af.ui.forms.FormViewObj.java

/**
 * Sets the focus to the first control in the form.
 *//*  www. j  a  v  a2  s.  co  m*/
public void focusFirstFormControl() {
    Component focusable = getFirstFocusable();
    if (focusable != null) {
        focusable.requestFocus();
    }
}

From source file:org.simmi.GeneSetHead.java

License:asdf

public static void copyData(Component source) {
    JTextArea textarea = (JTextArea) source;
    String s = textarea.getText();

    if (s == null || s.trim().length() == 0) {
        JOptionPane.showMessageDialog(source, "There is no data selected!");
    } else {//from w  w w.  j a  va2 s  . co  m
        StringSelection selection = new StringSelection(s);
        clipboardService.setContents(selection);
    }

    if (grabFocus) {
        source.requestFocus();
    }
}