Example usage for javax.swing JButton requestFocus

List of usage examples for javax.swing JButton requestFocus

Introduction

In this page you can find the example usage for javax.swing JButton requestFocus.

Prototype

public void requestFocus() 

Source Link

Document

Requests that this Component gets the input focus.

Usage

From source file:de.ipk_gatersleben.ag_nw.graffiti.services.GUIhelper.java

public static JComponent getWebsiteDownloadButton(final String title, final String optUrlManualDownloadWebsite,
        final String target_dir_null_ask_user, final String optIntroText, final String[] downloadURLs,
        final String optIntroDialogTitle, final FileDownloadStatusInformationProvider statusProvider,
        final Runnable optFinishSwingTask) {

    final JButton res = new JMButton("Download/Update");
    res.setToolTipText(//from w w w .  j av a  2  s.  co m
            "<html>Click button to start automatic download<br><code><b>Check License/Disclaimers first!");
    res.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final String opt_local_folder;
            if (target_dir_null_ask_user == null) {
                File file = OpenFileDialogService.getDirectoryFromUser("Select folder");
                if (file == null)
                    return;
                else
                    opt_local_folder = file.getAbsolutePath();
            } else {
                opt_local_folder = target_dir_null_ask_user;
            }

            res.setEnabled(false);
            res.setText("Downloading");
            final BackgroundTaskStatusProviderSupportingExternalCallImpl status = new BackgroundTaskStatusProviderSupportingExternalCallImpl(
                    "Please wait...", "Downloading files...");
            BackgroundTaskHelper.issueSimpleTask(title, "Please wait...", new Runnable() {
                public void run() {
                    boolean allOK = true;
                    for (String downloadURL : downloadURLs) {
                        allOK = performDownload(downloadURL, opt_local_folder, status);
                        if (status.wantsToStop()) {
                            break;
                        }
                    }
                    if (status.wantsToStop()) {
                        allOK = true;
                        try {
                            status.setCurrentStatusText1("Cancel...");
                            for (String downloadURL : downloadURLs) {
                                String fileName = downloadURL.substring(downloadURL.lastIndexOf("/") + 1);
                                if (downloadURL.contains("|"))
                                    fileName = downloadURL.substring(downloadURL.lastIndexOf("|") + 1);
                                String targetFileName = ReleaseInfo.getAppFolderWithFinalSep() + fileName;
                                if (new File(targetFileName).exists()) {
                                    new File(targetFileName).delete();
                                    status.setCurrentStatusText2("Delete " + targetFileName);
                                }
                            }
                        } catch (Exception e) {
                            //
                        }
                    }

                    if (!allOK) {
                        res.setEnabled(true);
                        res.setText(
                                "<html><small>Automatic download failure<br>Click here for manual download");
                        res.removeActionListener(res.getActionListeners()[0]);
                        res.addActionListener(getDialogAction(optUrlManualDownloadWebsite, opt_local_folder,
                                optIntroText, optIntroDialogTitle));
                        res.requestFocus();
                        final JDialog jd = (JDialog) ErrorMsg.findParentComponent(res, JDialog.class);
                        if (jd != null) {
                            SwingUtilities.invokeLater(new Runnable() {
                                public void run() {
                                    jd.pack();
                                }
                            });
                        }
                    } else {
                        if (status.wantsToStop()) {
                            res.setText("Canceled");
                            status.pleaseContinueRun();
                            status.setCurrentStatusText2("Canceled!");
                        } else {
                            res.setText("Downloaded");
                        }
                        res.setEnabled(false);
                        if (statusProvider != null)
                            statusProvider.finishedNewDownload();
                        if (optFinishSwingTask != null)
                            SwingUtilities.invokeLater(optFinishSwingTask);
                    }
                }
            }, null, status);
        }
    });
    return res;
}

From source file:com.haulmont.cuba.desktop.sys.DesktopWindowManager.java

protected JPanel createButtonsPanel(Action[] actions, final DialogWindow dialog) {
    JPanel buttonsPanel = new JPanel();

    boolean hasPrimaryAction = false;
    for (final Action action : actions) {
        JButton button = new JButton(action.getCaption());
        String icon = action.getIcon();

        if (icon != null) {
            button.setIcon(AppBeans.get(IconResolver.class).getIconResource(icon));
        }/*from   w w w.j a v a2  s.  c  om*/

        final DialogActionHandler dialogActionHandler = new DialogActionHandler(dialog, action);
        button.addActionListener(dialogActionHandler);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton b = (JButton) e.getSource();
                userActionsLog.trace("Button (name = {}, text = {}) was clicked in dialog", b.getName(),
                        b.getText());
            }
        });
        if (actions.length == 1) {
            dialog.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    dialogActionHandler.onClose();
                }
            });
        }

        button.setPreferredSize(
                new Dimension(button.getPreferredSize().width, DesktopComponentsHelper.BUTTON_HEIGHT));
        button.setMaximumSize(new Dimension(Integer.MAX_VALUE, DesktopComponentsHelper.BUTTON_HEIGHT));

        if (action instanceof AbstractAction && ((AbstractAction) action).isPrimary()) {
            hasPrimaryAction = true;

            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    button.requestFocus();
                }
            });
        }

        buttonsPanel.add(button);
    }

    if (!hasPrimaryAction && actions.length > 0) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                buttonsPanel.getComponent(0).requestFocus();
            }
        });
    }

    return buttonsPanel;
}

From source file:fxts.stations.util.preferences.EditAction.java

public void actionPerformed(ActionEvent aEvent) {
    JButton okButton = UIManager.getInst().createButton();
    JButton cancelButton = UIManager.getInst().createButton();
    final JDialog dialog = new JDialog(mEditorPanel.getParentDialog());
    dialog.setTitle(mEditorPanel.getTitle());
    JPanel editPanel = new JPanel();
    JPanel buttonPanel = new JPanel();
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(UIFrontEnd.getInstance().getSideLayout());
    //mainPanel.setLayout(new SideLayout());

    //sets button panel
    buttonPanel.setLayout(UIFrontEnd.getInstance().getSideLayout());
    okButton.setText(mResMan.getString("IDS_OK_BUTTON"));
    //okButton.setPreferredSize(new Dimension(80, 27));
    GridBagConstraints sideConstraints = UIFrontEnd.getInstance().getSideConstraints();
    sideConstraints.insets = new Insets(10, 10, 10, 10);
    sideConstraints.gridx = 0;/*from  www . ja  v a 2 s .  c om*/
    sideConstraints.gridy = 0;
    ResizeParameterWrapper resizeParameter = UIFrontEnd.getInstance().getResizeParameter();
    resizeParameter.init(0.5, 0.0, 0.5, 0.0);
    resizeParameter.setToConstraints(sideConstraints);
    buttonPanel.add(okButton, sideConstraints);
    cancelButton.setText(mResMan.getString("IDS_CANCEL_BUTTON"));
    //cancelButton.setPreferredSize(new Dimension(80, 27));
    sideConstraints = UIFrontEnd.getInstance().getSideConstraints();
    sideConstraints.insets = new Insets(10, 10, 10, 10);
    sideConstraints.gridx = 1;
    sideConstraints.gridy = 0;
    resizeParameter = UIFrontEnd.getInstance().getResizeParameter();
    resizeParameter.init(0.5, 0.0, 0.5, 0.0);
    resizeParameter.setToConstraints(sideConstraints);
    buttonPanel.add(cancelButton, sideConstraints);

    //adds button panel
    sideConstraints = UIFrontEnd.getInstance().getSideConstraints();
    sideConstraints.insets = new Insets(10, 10, 10, 10);
    sideConstraints.gridx = 0;
    sideConstraints.gridy = 1;
    resizeParameter = UIFrontEnd.getInstance().getResizeParameter();
    resizeParameter.init(0.0, 1.0, 1.0, 1.0);
    resizeParameter.setToConstraints(sideConstraints);
    mainPanel.add(buttonPanel, sideConstraints);

    //sets edit panel
    final IEditor editor = mType.getEditor();
    editor.setValue(mValue);
    editPanel.setLayout(UIFrontEnd.getInstance().getSideLayout());
    sideConstraints = UIFrontEnd.getInstance().getSideConstraints();
    resizeParameter = UIFrontEnd.getInstance().getResizeParameter();
    resizeParameter.init(0.0, 0.0, 1.0, 1.0);
    resizeParameter.setToConstraints(sideConstraints);
    Component editComp = editor.getComponent();

    //Mar 25 2004 - kav: added for right tab order at Font Chooser at java 1.4.
    if (editComp instanceof FontChooser) {
        FontChooser fc = (FontChooser) editComp;
        fc.setNextFocusedComp(okButton);
    }
    editPanel.add(editComp, sideConstraints);

    //adds editor panel
    sideConstraints = UIFrontEnd.getInstance().getSideConstraints();
    sideConstraints.gridx = 0;
    sideConstraints.gridy = 0;
    resizeParameter = UIFrontEnd.getInstance().getResizeParameter();
    resizeParameter.init(0.0, 0.0, 1.0, 1.0);
    resizeParameter.setToConstraints(sideConstraints);
    mainPanel.add(editPanel, sideConstraints);

    //adds main panel
    dialog.getContentPane().setLayout(UIFrontEnd.getInstance().getSideLayout());
    //dialog.getContentPane().setLayout(new SideLayout());
    sideConstraints = UIFrontEnd.getInstance().getSideConstraints();
    sideConstraints.fill = GridBagConstraints.BOTH;
    resizeParameter = UIFrontEnd.getInstance().getResizeParameter();
    resizeParameter.init(0.0, 0.0, 1.0, 1.0);
    resizeParameter.setToConstraints(sideConstraints);
    dialog.getContentPane().add(mainPanel, sideConstraints);

    //adds listeners to buttons
    okButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent aEvent) {
            if (editor.getValue().equals(mValue)) {
                //
            } else {
                mValue = editor.getValue();
                mEditorPanel.setValue(mValue);
                mEditorPanel.refreshControls();
                mEditorPanel.setValueChanged(true);
            }
            dialog.setVisible(false);
            dialog.dispose();
        }
    });
    okButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
            "ExitAction");
    okButton.getActionMap().put("ExitAction", new AbstractAction() {
        /**
         * Invoked when an action occurs.
         */
        public void actionPerformed(ActionEvent aEvent) {
            editor.setValue(mValue);
            dialog.setVisible(false);
            dialog.dispose();
        }
    });
    okButton.requestFocus();
    cancelButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent aEvent) {
            editor.setValue(mValue);
            dialog.setVisible(false);
            dialog.dispose();
        }
    });
    //dialog.setResizable(false);
    dialog.setModal(true);
    dialog.pack();

    //sets minimal sizes for components
    Dimension dim = mainPanel.getSize();
    mainPanel.setMinimumSize(dim);
    mainPanel.setPreferredSize(dim);

    //sets size of buttons
    Dimension dimOkButton = okButton.getSize();
    Dimension dimCancelButton = cancelButton.getSize();
    int nMaxWidth = dimOkButton.getWidth() > dimCancelButton.getWidth() ? (int) dimOkButton.getWidth()
            : (int) dimCancelButton.getWidth();
    okButton.setPreferredSize(new Dimension(nMaxWidth, (int) dimOkButton.getHeight()));
    okButton.setSize(new Dimension(nMaxWidth, (int) dimOkButton.getHeight()));
    cancelButton.setPreferredSize(new Dimension(nMaxWidth, (int) dimCancelButton.getHeight()));
    cancelButton.setSize(new Dimension(nMaxWidth, (int) dimCancelButton.getHeight()));
    dialog.setLocationRelativeTo(dialog.getOwner());
    dialog.setVisible(true);
}