Setting component focus in JOptionPane.showOptionDialog()

前端 未结 8 1521
广开言路
广开言路 2020-12-10 13:59

In order to have custom button captions in an input dialog, I created the following code:

String key = null;
JTextField txtKey = new JTextField();        
in         


        
8条回答
  •  悲哀的现实
    2020-12-10 14:06

    I had the same problem with the RequestFocusListener() not working on Linux, after following the discussion on http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5018574 I found that adding an invokeLater fixed it for now...

    public class RequestFocusListener implements AncestorListener
    {
    public void ancestorAdded(final AncestorEvent e)
    {
        final AncestorListener al= this;   
        SwingUtilities.invokeLater(new Runnable(){
    
            @Override
            public void run() {
                JComponent component = (JComponent)e.getComponent();
                component.requestFocusInWindow();
                component.removeAncestorListener( al );
            }
        });
    }
    
    public void ancestorMoved(AncestorEvent e) {}
    public void ancestorRemoved(AncestorEvent e) {}
    }
    

提交回复
热议问题