Results 1 to 6 of 6
- 10-15-2009, 01:33 PM #1
Member
- Join Date
- Aug 2007
- Posts
- 44
- Rep Power
- 0
JPasswordTextField using JOptionPane
I want to use JPasswordTextField in JOptionPane.showInputMessage(...) dialog instead of textfield component.
I think that It may be possible using createDialog() Method of JOptionPane OR OptionPaneUI! but how?
that is I want to use an Password Dialog Box like Input Dialog of JOptionPane.
Plese tell me that how can I do this?
Thanks in advance...
- 10-15-2009, 02:23 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Use JDialog directly and create your own.
- 10-17-2009, 07:42 AM #3
1. There's a Swing section where this should have been posted.
2. There's no class called JPasswordTextField in the standard API.
3. Any component can be passed as the "message" argument to showMessageDialog.
db
- 10-18-2009, 06:58 AM #4
Member
- Join Date
- Aug 2007
- Posts
- 44
- Rep Power
- 0
[SOVED] By Alternative Way
Darryl.Burke:
Yes, I too agree with you. I must fillow this rule in future.There's a Swing section where this should have been posted.
Darryl.Burke:SORRY! there is a class JPasswordField in JAVA API that I wanted to mention.There's no class called JPasswordTextField in the standard API.
masijade:I had that idea in my mind but I put this question b'coz I just wanted to know the concepts of "JOptionPane component settings" and "how to use OptionPaneUI"Use JDialog directlys and create your own.
I created my own showPasswordDialog(...) function which is attached. Rename the MyPasswordFieldText.txt to MyPasswordFieldText.java and run it.
Darryl.Burke:Thanks, but my purpose is not soved using this method.Any component can be passed as the "message" argument to showMessageDialog
e.g.
then I will not get password which will be typed in password field....kindly update my further knowledge regarding passing component as message.Java Code:String password=JOptionPane.showInputDialog(null,new JPasswordField(20)); System.out.println(password);
What is the use of it?
-
Java Code:import javax.swing.*; public class MyPass2 { public MyPass2() { } private static void createAndShowUI() { JPasswordField pField = new JPasswordField(10); JPanel pPanel = new JPanel(); pPanel.add(new JLabel("Please Enter Password: ")); pPanel.add(pField); int result = JOptionPane.showConfirmDialog(null, pPanel); if (result == JOptionPane.OK_OPTION) { System.out.println(String.valueOf(pField.getPassword())); } } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
- 10-19-2009, 11:02 AM #6
Member
- Join Date
- Aug 2007
- Posts
- 44
- Rep Power
- 0
Thanks
Thanks for told me the uses of passing controls to JOptionPane.show*(...) functions.
I modified your code as below:-
Only problem is that default focus is OK button istead of JPasswordField;Java Code:import javax.swing.*; import java.awt.GridBagLayout; import java.awt.GridBagConstraints; public class MyPass2 { public MyPass2() { } private static void createAndShowUI() { JPasswordField pField = new JPasswordField(20); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); c.insets.top = 4; c.insets.bottom = 4; JPanel pPanel = new JPanel(gridbag); pPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 5, 20)); c.anchor = GridBagConstraints.WEST; pPanel.add(new JLabel("Please Enter Password: "),c); c.gridy=1; pPanel.add(pField,c); int result = JOptionPane.showConfirmDialog(null, pPanel,"Password",JOptionPane.OK_CANCEL_OPTION); if (result == JOptionPane.OK_OPTION) { System.out.println(String.valueOf(pField.getPassword())); } } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
If we add following lines then we will get focus properly:-
but we need to write action listener for ok button to find out wheather OK button is pressed or not?Java Code:JOptionPane jp=new JOptionPane(pPanel,JOptionPane.OK_CANCEL_OPTION); pPanel.requestFocusInWindow(); jp.createDialog().setVisible(true);
please modify my above code such that I will get default focus to password field.Last edited by Gajesh Tripathi; 10-19-2009 at 11:19 AM.
Similar Threads
-
help with JOptionPane.showMessageDialog()
By gmn1 in forum New To JavaReplies: 8Last Post: 03-30-2009, 09:31 AM -
JOptionpane
By tommyyyy in forum New To JavaReplies: 2Last Post: 03-20-2009, 08:33 AM -
JOptionPane.showInputDialog
By mayhewj7 in forum New To JavaReplies: 5Last Post: 02-25-2009, 06:04 AM -
JOptionPane
By Mir in forum New To JavaReplies: 5Last Post: 11-29-2008, 02:04 AM -
JOptionPane
By whosadork in forum New To JavaReplies: 2Last Post: 10-23-2008, 02:17 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks