Reputation: 3554
I have done code below but some how I am not able to set focus on JDialog :( loginDialog.requestFocusInWindow() returns false. Is there is any way to get focus on JDialog? LoginDialog.this.txt_PASSWORD.requestFocusInWindow() also returns false.
Esc button press event is also not working
this.loginDialog = new JDialog();
this.loginDialog.setTitle(applicationName+Keys.BLANK+Keys.DASH+Keys.BLANK+Messages.getMessage(IMessageKeys.LOGIN));
this.loginDialog.setModal(true);
this.loginDialog.setLayout(new BorderLayout());
this.loginPanel=getLoginPane();
this.buttonPanel=getButtonPanel();
this.infoLabel.setText(Keys.BLANK);
this.loginDialog.add(this.infoLabel,BorderLayout.NORTH);
this.loginDialog.add(this.loginPanel,BorderLayout.CENTER);
this.loginDialog.add(this.buttonPanel,BorderLayout.SOUTH);
this.loginDialog.setSize(370, 236);
this.loginDialog.setResizable(false);
this.loginDialog.setLocationRelativeTo(null);
objLogger.debug("Login dialog init method call end"); //$NON-NLS-1$
this.loginDialog.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
LoginDialog.this.loginDialog.requestFocus();
LoginDialog.this.loginDialog.requestFocusInWindow();
LoginDialog.this.txt_PASSWORD.addNotify();
LoginDialog.this.txt_PASSWORD.requestFocusInWindow();
LoginDialog.this.txt_PASSWORD.requestFocus();
}
@Override
public void windowClosing(WindowEvent e) {
close();
}
});
KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true);
this.loginDialog.getRootPane().getInputMap().put(ks, GenePanelConstants.CLOSE_ACTION);
this.loginDialog.getRootPane().getActionMap().put( GenePanelConstants.CLOSE_ACTION, new AbstractAction() {
private static final long serialVersionUID = 2871751669355251894L;
@Override
public void actionPerformed(ActionEvent ae) {
close();
}
});
this.txt_PASSWORD.requestFocusInWindow();
this.txt_PASSWORD.requestFocus();
this.loginDialog.setAlwaysOnTop(true);
this.loginDialog.setVisible(true);
Upvotes: 2
Views: 124
Reputation: 3554
I got the some idea of it. As the focus management is managed by the system. You can get the focus on your dialog if and only if, it is the only one editable active window present in system.
For example, Keep the eclipse open which has cursor blinking on its editable window then start your application, text-box present in your application will not get focus( Even after you launch the application, the cursor will be blinking on editable window of eclipse). In same scenario if there is no blinking cursor on editable window of eclipse, your application text-box will get focused.
Upvotes: 1