Sandeep & Vicky’s Archive ยป Blog Archive » How to alter the default focus option in a JOptionPane?

I was trying to set default focus to a textfield which was placed in a joptionpane, but it wast working with the following code

cvendor.requestFocus();

whereas the same code worked perfectly when the textfield was placed in a jpanel. On research i found that the default focus in a joptionpane is always on the OK button and it cannot be changed statically, it can only b changed during runtime using the Runnable interface. Here is the code that did the trick

javax.swing.SwingUtilities.invokeLater(new Runnable() {//<—–add
public void run() { cvendor.requestFocus(); }});//<—-add

nn=JOptionPane.showConfirmDialog(null,Jop,”Please enter details”,JOptionPane.CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);

kool na………..

Related Posts with Thumbnails
This entry was posted on Friday, October 9th, 2009 at 7:47 pm and is filed under java. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a reply

Name (*)
Mail (will not be published) (*)
URI
Comment