Example usage for javax.swing JButton requestFocusInWindow

List of usage examples for javax.swing JButton requestFocusInWindow

Introduction

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

Prototype

public boolean requestFocusInWindow() 

Source Link

Document

Requests that this Component gets the input focus.

Usage

From source file:de.huxhorn.lilith.swing.LicenseAgreementDialog.java

private void initUI() {
    JPanel content = new JPanel();
    content.setLayout(new BorderLayout());
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

    JTextPane licenseTextPane = new JTextPane();
    HTMLEditorKit htmlEditorKit = new HTMLEditorKit();

    String licenseText = null;/*from  www  .  j a  v  a 2s.c o m*/
    InputStream licenseStream = LicenseAgreementDialog.class.getResourceAsStream("/licenses/license.html");
    if (licenseStream != null) {
        try {
            licenseText = IOUtils.toString(licenseStream, StandardCharsets.UTF_8);
        } catch (IOException ex) {
            if (logger.isErrorEnabled())
                logger.error("Exception while loading license!", ex);
            IOUtilities.interruptIfNecessary(ex);
        } finally {
            IOUtilities.closeQuietly(licenseStream);
        }
    }
    if (licenseText == null) {
        if (logger.isErrorEnabled())
            logger.error("Couldn't find license text! Exiting!");
        System.exit(-1);
    }

    licenseTextPane.setEditorKit(htmlEditorKit);
    licenseTextPane.setEditable(false);
    licenseTextPane.setText(licenseText);
    JScrollPane licenseScrollPane = new JScrollPane(licenseTextPane);
    licenseScrollPane.setPreferredSize(new Dimension(640, 480));

    content.add(licenseScrollPane, BorderLayout.CENTER);
    content.add(buttonPanel, BorderLayout.SOUTH);

    AcceptAction acceptAction = new AcceptAction();
    DeclineAction declineAction = new DeclineAction();

    JButton acceptButton = new JButton(acceptAction);
    JButton declineButton = new JButton(declineAction);

    buttonPanel.add(acceptButton);
    buttonPanel.add(declineButton);
    setContentPane(content);
    licenseTextPane.setCaretPosition(0);
    declineButton.requestFocusInWindow();
}

From source file:com.aw.swing.mvp.JDialogView.java

private void initializeDlg() {
    if (pst.isEmbeddedView()) {
        return;//ww w .  j  av a  2s.c  o  m
    }
    JDialog dlg = (JDialog) parentContainer;

    dlg.add(viewLayout.mainPanel, BorderLayout.CENTER);

    //        SwingUtils.locateOnScreenCenter(dlg);
    SwingUtils.locateRelativeToMenu(dlg);
    dlg.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    dlg.addWindowListener(new java.awt.event.WindowAdapter() {
        // todo ver cmo se va a hacer esto en la versin Filthy
        public void windowOpened(WindowEvent e) {
            if (pst != null) {
                pst.onWindowsOpened(e);
                pst.onWindowsOpenedInternalOnlyForAWFW(e);
                if (!pst.isReadOnly()) {
                    //                        pst.setFocusToCmpOnWindowOpen();
                    //                        ActionManager.instance().getVisualMgrForActions().repaintMainDisabledActions(pst.getActionRsr());
                } else {
                    pst.configureAsReanOnly();
                    JButton btnCancel = (JButton) pst.getIpView().getComponent("btnCancel");
                    if (btnCancel != null) {
                        btnCancel.requestFocusInWindow();
                    }
                }
            }
        }

        public void windowClosing(WindowEvent e) {
            MsgDisplayer.showMessage("sw.common.closeWindowDisabled");
        }

        public void windowActivated(WindowEvent e) {
            Object oppositeWindow = e.getOppositeWindow();

            if (oppositeWindow instanceof DlgMensaje) {
                logger.debug("Ignoring Windows activated for DlgMensaje");
                return;
            }
            if (oppositeWindow instanceof JDialog) {
                JDialog dlg = (JDialog) oppositeWindow;
                if (MessageDisplayer.GENERIC_MESSAGE_TITLE.equals(dlg.getTitle())) {
                    logger.debug("Ignoring Windows activated for System Msg");
                    return;
                }
            }
            logger.debug("Windows Activated:" + pst);
            AWWindowsManager.instance().setPresenterActivated(pst);
        }
    });
}

From source file:net.pandoragames.far.ui.swing.FindFilePanel.java

private void initBaseDirPanel(SwingConfig config, ComponentRepository componentRepository) {

    this.add(Box.createRigidArea(new Dimension(1, SwingConfig.PADDING)));

    JLabel labelBaseDir = new JLabel(config.getLocalizer().localize("label.base-directory"));
    labelBaseDir.setAlignmentX(Component.LEFT_ALIGNMENT);
    this.add(labelBaseDir);

    baseDirPathTextField//from   w  w w  . ja v a2s .  co  m
            .setPreferredSize(new Dimension(SwingConfig.COMPONENT_WIDTH, config.getStandardComponentHight()));
    baseDirPathTextField
            .setMaximumSize(new Dimension(SwingConfig.COMPONENT_WIDTH_MAX, config.getStandardComponentHight()));
    // baseDirPathTextField.setText( dataModel.getBaseDirectory().getPath() );
    baseDirPathTextField.addItem(dataModel.getBaseDirectory().getPath());
    baseDirPathTextField.setSelectedItem(dataModel.getBaseDirectory().getPath());
    baseDirPathTextField.setToolTipText(dataModel.getBaseDirectory().getPath());
    baseDirPathTextField.setEditable(false);
    baseDirPathTextField.addItemListener(new BaseDirectoryComboBoxListener(config, dataModel));

    JButton openBaseDirFileChooserButton = new JButton(localizer.localize("button.browse"));
    openBaseDirFileChooserButton.requestFocusInWindow();
    class BaseDirectoryRepository extends AbstractFileRepository {
        public BaseDirectoryRepository(FARConfig cfg, ComponentRepository repository) {
            super(cfg, repository.getFindForm(), repository.getReplaceForm(), repository.getMessageBox());
        }

        public File getFile() {
            return farconfig.getBaseDirectory();
        }

        public boolean setFile(File file) {
            if (isSubdirectory(replaceForm.getBackupDirectory(), file)) {
                messageBox.error(localizer.localize("message.nested-base-child"));
                return false;
            } else if (isSubdirectory(file, replaceForm.getBackupDirectory())) {
                messageBox.error(localizer.localize("message.nested-base-parent"));
                return false;
            } else {
                baseDirPathTextField.addItem(file.getPath());
                baseDirPathTextField.setSelectedItem(file.getPath());
                baseDirPathTextField.setToolTipText(file.getPath());
                farconfig.setBaseDirectory(file);
                findForm.setBaseDirectory(file);
                return true;
            }
        }
    }
    browseButtonListener = new FindFilePanelBrowseButtonListener(baseDirPathTextField,
            new BaseDirectoryRepository(config, componentRepository),
            localizer.localize("label.choose-base-directory"), componentRepository.getFindCommand(),
            componentRepository.getResetDispatcher());
    browseButtonListener.addActionListener(componentRepository.getSearchBaseListener());
    openBaseDirFileChooserButton.addActionListener(browseButtonListener);
    TwoComponentsPanel lineBaseDir = new TwoComponentsPanel(baseDirPathTextField, openBaseDirFileChooserButton);
    lineBaseDir.setAlignmentX(Component.LEFT_ALIGNMENT);
    this.add(lineBaseDir);

    subdirFlag = new JCheckBox(localizer.localize("label.include-subdir") + ":");
    subdirFlag.setSelected(dataModel.isIncludeSubDirs());
    subdirFlag.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent event) {
            dataModel.setIncludeSubDirs((ItemEvent.SELECTED == event.getStateChange()));
            if (ItemEvent.SELECTED != event.getStateChange())
                subdirButton.reset();
        }
    });
    subdirFlag.addItemListener(componentRepository.getSearchBaseListener());
    componentRepository.getResetDispatcher().addToBeSelected(subdirFlag);
    browseButtonListener.setSubdirCheckBox(subdirFlag);
    subdirButton = new SubdirPatternLink(dataModel, localizer, componentRepository.getSearchBaseListener(),
            componentRepository.getRootWindow());
    componentRepository.getResetDispatcher().addResetable(subdirButton);
    TwoComponentsPanel subdirpanel = new TwoComponentsPanel(subdirFlag, subdirButton);
    subdirpanel.setAlignmentX(Component.LEFT_ALIGNMENT);
    this.add(subdirpanel);

    this.add(Box.createRigidArea(new Dimension(1, SwingConfig.PADDING)));
    this.add(Box.createVerticalGlue());
}

From source file:eu.lp0.cursus.ui.AboutDialog.java

private void initialise() {
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    setTitle(Messages.getString("about.title", Constants.APP_DESC)); //$NON-NLS-1$
    DefaultUnitConverter duc = DefaultUnitConverter.getInstance();

    FormLayout layout = new FormLayout("2dlu, pref, fill:pref:grow, max(30dlu;pref), 2dlu", //$NON-NLS-1$
            "2dlu, max(15dlu;pref), 2dlu, max(15dlu;pref), 2dlu, fill:max(100dlu;pref):grow, 2dlu, max(16dlu;pref), 2dlu"); //$NON-NLS-1$
    getContentPane().setLayout(layout);//from  w  ww.jav  a 2 s. co  m

    JLabel lblName = new JLabel(Constants.APP_NAME + ": " + Messages.getString("about.description")); //$NON-NLS-1$ //$NON-NLS-2$
    getContentPane().add(lblName, "2, 2, 3, 1"); //$NON-NLS-1$

    getContentPane().add(new LinkJButton(Constants.APP_URL), "2, 4"); //$NON-NLS-1$

    JScrollPane scrCopying = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    getContentPane().add(scrCopying, "2, 6, 3, 1"); //$NON-NLS-1$

    JTextArea txtCopying = new JTextArea(loadResources("COPYRIGHT", "LICENCE")); //$NON-NLS-1$ //$NON-NLS-2$
    txtCopying.setFont(Font.decode(Font.MONOSPACED));
    txtCopying.setEditable(false);
    scrCopying.setViewportView(txtCopying);
    scrCopying.setPreferredSize(
            new Dimension(scrCopying.getPreferredSize().width, duc.dialogUnitYAsPixel(100, scrCopying)));

    Action actClose = new CloseDialogAction(this);
    JButton btnClose = new JButton(actClose);
    getContentPane().add(btnClose, "4, 8"); //$NON-NLS-1$

    getRootPane().setDefaultButton(btnClose);
    getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
            .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), CloseDialogAction.class.getName());
    getRootPane().getActionMap().put(CloseDialogAction.class.getName(), actClose);

    pack();
    setMinimumSize(getSize());
    setSize(getSize().width, getSize().height * 3 / 2);
    btnClose.requestFocusInWindow();
}

From source file:org.apache.marmotta.splash.common.ui.SelectionDialog.java

public static int select(String title, String message, String description, List<Option> options,
        int defaultOption) {
    final JDialog dialog = new JDialog((Frame) null, title);
    dialog.setModal(true);//w w  w .j a  va2 s  . c o m
    dialog.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

    final AtomicInteger result = new AtomicInteger(Math.max(defaultOption, -1));

    JButton defaultBtn = null;

    final JPanel root = new JPanel(new GridBagLayout());
    root.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    dialog.getRootPane().setContentPane(root);

    JLabel lblMsg = new JLabel("<html>" + StringEscapeUtils.escapeHtml3(message).replaceAll("\\n", "<br>"));
    lblMsg.setFont(lblMsg.getFont().deriveFont(Font.BOLD, 16f));
    GridBagConstraints cLabel = new GridBagConstraints();
    cLabel.gridx = 0;
    cLabel.gridy = 0;
    cLabel.fill = GridBagConstraints.BOTH;
    cLabel.weightx = 1;
    cLabel.weighty = 0.5;
    cLabel.insets = new Insets(5, 5, 5, 5);
    root.add(lblMsg, cLabel);

    JLabel lblDescr = new JLabel(
            "<html>" + StringEscapeUtils.escapeHtml3(description).replaceAll("\\n", "<br>"));
    cLabel.gridy++;
    cLabel.insets = new Insets(0, 5, 5, 5);
    root.add(lblDescr, cLabel);

    // All the options
    cLabel.ipadx = 10;
    cLabel.ipady = 10;
    cLabel.insets = new Insets(5, 15, 0, 15);
    for (int i = 0; i < options.size(); i++) {
        cLabel.gridy++;

        final Option o = options.get(i);
        final JButton btn = new JButton(
                "<html>" + StringEscapeUtils.escapeHtml3(o.label).replaceAll("\\n", "<br>"),
                MessageDialog.loadIcon(o.icon));
        if (StringUtils.isNotBlank(o.info)) {
            btn.setToolTipText("<html>" + StringEscapeUtils.escapeHtml3(o.info).replaceAll("\\n", "<br>"));
        }

        btn.setHorizontalAlignment(AbstractButton.LEADING);
        btn.setVerticalTextPosition(AbstractButton.CENTER);
        btn.setHorizontalTextPosition(AbstractButton.TRAILING);

        final int myAnswer = i;
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                result.set(myAnswer);
                dialog.setVisible(false);
            }
        });

        root.add(btn, cLabel);
        if (i == defaultOption) {
            dialog.getRootPane().setDefaultButton(btn);
            defaultBtn = btn;
        }
    }

    final Icon icon = MessageDialog.loadIcon();
    if (icon != null) {
        JLabel lblIcn = new JLabel(icon);

        GridBagConstraints cIcon = new GridBagConstraints();
        cIcon.gridx = 1;
        cIcon.gridy = 0;
        cIcon.gridheight = 2 + options.size();
        cIcon.fill = GridBagConstraints.NONE;
        cIcon.weightx = 0;
        cIcon.weighty = 1;
        cIcon.anchor = GridBagConstraints.NORTH;
        cIcon.insets = new Insets(10, 5, 5, 0);
        root.add(lblIcn, cIcon);
    }

    final JButton close = new JButton("Cancel");
    close.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            result.set(-1);
            dialog.setVisible(false);
        }
    });
    GridBagConstraints cClose = new GridBagConstraints();
    cClose.gridx = 0;
    cClose.gridy = 2 + options.size();
    cClose.gridwidth = 2;
    cClose.weightx = 1;
    cClose.weighty = 0;
    cClose.insets = new Insets(15, 5, 5, 5);

    root.add(close, cClose);
    if (defaultOption < 0) {
        dialog.getRootPane().setDefaultButton(close);
        defaultBtn = close;
    }

    dialog.pack();
    dialog.setLocationRelativeTo(null);
    defaultBtn.requestFocusInWindow();

    dialog.setVisible(true);
    dialog.dispose();

    return result.get();
}

From source file:org.rdv.ui.ConsoleDialog.java

public ConsoleDialog(JDialog owner) {
    super(owner);

    setName("consoleDialog");

    setDefaultCloseOperation(AboutDialog.DISPOSE_ON_CLOSE);

    JPanel container = new JPanel();
    container.setLayout(new BorderLayout());
    container.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    setContentPane(container);/* w w w  .  j a  v  a 2  s .  c om*/

    InputMap inputMap = container.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap actionMap = container.getActionMap();

    Action disposeAction = new AbstractAction() {
        /** serialized version identifier */
        private static final long serialVersionUID = 4380189911762232261L;

        public void actionPerformed(ActionEvent ae) {
            dispose();
        }
    };

    Action copyAction = new AbstractAction() {

        /** serialized version identifier */
        private static final long serialVersionUID = 2596081241883913660L;

        public void actionPerformed(ActionEvent e) {
            textArea.selectAll();
            textArea.copy();
        }
    };

    //    Action scrollLockAction = new AbstractAction() {
    //      /** serialized version identifier */
    //      private static final long serialVersionUID = -8089076016097529064L;
    //
    //      public void actionPerformed(ActionEvent e) {
    //        //toggle scroll lock
    //        scrollLock_=!scrollLock_;
    //      }
    //    };

    disposeAction.putValue(Action.NAME, "OK");
    inputMap.put(KeyStroke.getKeyStroke("ENTER"), "dispose");
    inputMap.put(KeyStroke.getKeyStroke("ESCAPE"), "dispose");
    actionMap.put("dispose", disposeAction);
    copyAction.putValue(Action.NAME, "Copy");
    actionMap.put("copy", copyAction);
    //    actionMap.put("scroll lock", scrollLockAction);
    //    scrollLockAction.putValue(Action.NAME, "Scroll Lock");

    textArea = new JTextArea();
    textArea.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    textArea.setBackground(Color.WHITE);
    textArea.setForeground(Color.BLACK);
    textArea.setEditable(false);
    textArea.setLineWrap(false);

    Iterator<String> msgIt = messageBuffer.getMessages().iterator();

    //add all the messages in the buffer to this point
    while (msgIt.hasNext()) {
        addMessage(msgIt.next());
    }

    JScrollPane scrollPane = new JScrollPane(textArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPane.setPreferredSize(new Dimension(640, 480));
    container.add(scrollPane, BorderLayout.CENTER);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BorderLayout());
    buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    JButton okButton = new JButton(disposeAction);
    buttonPanel.add(okButton, BorderLayout.EAST);

    JPanel leftBtnsPanel = new JPanel();
    leftBtnsPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    buttonPanel.add(leftBtnsPanel, BorderLayout.WEST);

    JButton copyButton = new JButton(copyAction);
    leftBtnsPanel.add(copyButton);

    //    JButton scrollLockButton = new JButton(scrollLockAction);
    //    leftBtnsPanel.add(scrollLockButton);

    container.add(buttonPanel, BorderLayout.SOUTH);

    // inject resources from the properties for this component
    ResourceMap resourceMap = RDV.getInstance().getContext().getResourceMap(getClass());
    resourceMap.injectComponents(this);

    pack();
    okButton.requestFocusInWindow();
    setLocationByPlatform(true);
    setVisible(true);

    messageBuffer.addObserver(this);
}

From source file:org.stanwood.swing.AboutDialog.java

private void createButtonPane(Box box) {
    Box hBox = Box.createHorizontalBox();
    hBox.add(Box.createHorizontalGlue());
    JButton cmdClose = new JButton(StandardActions.getDialogCloseAction(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            cancel();/*  w  ww  . j ava  2s . c  o m*/
        }
    }));
    hBox.add(cmdClose);
    box.add(hBox);

    hBox.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));

    cmdClose.requestFocusInWindow();
}