Results 1 to 2 of 2
- 11-09-2009, 09:55 AM #1
Member
- Join Date
- Aug 2007
- Posts
- 44
- Rep Power
- 0
Default Focus in JOptionPane Dialog
When I run function showPasswordDialog(...) then default focus is OK button where as I want default focus password text field.
Please see the comment = //DOES NOT WORK
Kindly modify my code such that it works properly.
Java Code:import java.awt.*; import javax.swing.*; import java.awt.event.*; class TestShowPwdDialog { /* * Prompts the user for input a password with icon in a blocking dialog where the initial value, dialog title canbe specified. * User can input password which is required for further processing, usually by means of a <code>JTextField</code>. * <code>initalValue</code> is the initial value that can be used in cased of Saved Password * @return user's input password, or <code>null</code> meaning the user canceled the input */ public static String showPasswordDialog(Frame owner,String message,String initialValue,String title) { final JPasswordField pField = new JPasswordField(20); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); final JPanel pPanel; c.insets.top = 4; c.insets.bottom = 4; pField.setText(initialValue); pPanel = new JPanel(gridbag); pPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 5, 20)); c.anchor = GridBagConstraints.WEST; pPanel.add(new JLabel(message),c); pField.addFocusListener(new FocusListener() { public void focusLost(FocusEvent fe) { pField.setBackground(Color.white); } public void focusGained(FocusEvent fe) { pField.setBackground(Color.yellow); } }); c.gridy=1; pPanel.add(pField,c); pPanel.requestFocusInWindow(); //DOES NOT WORKS int result = JOptionPane.showConfirmDialog(owner, pPanel,title,JOptionPane.OK_CANCEL_OPTION); if (result == JOptionPane.OK_OPTION) return String.valueOf(pField.getPassword()); return null; } public static void main(String []args) { System.out.println(showPasswordDialog(null,"Enter user password:",null,"User Password")); } }
- 11-09-2009, 12:15 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Please don't post the same thread multiple times. I've close this.
Similar Threads
-
Default Focus in JOptionPane Dialog
By Gajesh Tripathi in forum AWT / SwingReplies: 5Last Post: 11-11-2009, 02:59 PM -
How to handle each display/shell dialog box (not knowed) and choose default answer
By lenar in forum SWT / JFaceReplies: 0Last Post: 04-04-2008, 12:15 AM -
JOptionPane dialog (Localizing)
By Java Tip in forum Java TipReplies: 0Last Post: 03-14-2008, 11:36 AM -
JOptionPane - message dialog
By Java Tip in forum Java TipReplies: 0Last Post: 12-17-2007, 09:11 AM -
JOptionPane - input dialog
By Java Tip in forum Java TipReplies: 0Last Post: 12-17-2007, 09:09 AM


LinkBack URL
About LinkBacks

Bookmarks