How can I request focus on a component in JOptionPane.showConfirmDialog?

Using JOptionPane.showConfirmDialogwith a custom component ( JPanel), I like to focus on a specific component ( JPasswordField) as it opens. How can this be achieved?

Code example: JPasswordField pf should have focus when opening dialogs ...

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;


public class JTestOptionDialog {

  public static void main(String[] args) {  
    JFrame frame = new JFrame("Test showConfirmDialog");
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(new JPanel());
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    JLabel label = new JLabel("<html><body>To access insert <b>password</b></body></html>");
    JPasswordField pf = new JPasswordField();
    JPanel panel = new JPanel(new GridBagLayout());
    panel.add(label,new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
    panel.add(pf,new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
    pf.requestFocus(); //THIS IS WHAT I LIKE TO HAVE FOCOUS WHEN DIALOG OPENS
    int retVal = JOptionPane.showConfirmDialog(frame,panel,"Impostazioni",JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
    System.out.println(retVal);
  }
}

Is there any way to request a focus on what JPasswordField pfI want, or do I need to "Create and use JOptionPane directly"?

As a note, I tried (which seemed logical)

pf.addComponentListener(new ComponentAdapter(){
  public void componentShown(ComponentEvent ce){
    pf.requestFocus(); //pf.requestFocusInWindow();
  }
});

But no luck with that ...

+4
source share
2 answers

, , (/), / ...

EDIT: invokeLater @mKorbel

pf.addAncestorListener(new AncestorListener() {     

        public void ancestorRemoved(AncestorEvent event) {}

        public void ancestorMoved(AncestorEvent event) {}            

        public void ancestorAdded(final AncestorEvent event) {
            final AncestorListener listener = this;
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    event.getComponent().requestFocusInWindow();
                    event.getComponent().removeAncestorListener(listener);
                }
            });
        }
});
+4

. , / . .

:

pf.addAncestorListener(new AncestorListener() {
    @Override
    public void ancestorRemoved(AncestorEvent event) {}

    @Override
    public void ancestorMoved(AncestorEvent event) {}

    @Override
    public void ancestorAdded(AncestorEvent event) {
        event.getComponent().requestFocusInWindow();
    }
});

, , JOptionPane.showConfirmDialog()

, JOptionPane.showOptionDialog()

+2

Source: https://habr.com/ru/post/1618122/


All Articles