2012-03-29 12 views
6

JOptionペインのshowInputDialogを使用して文字を非表示にする方法。例:JOptionPane.showInputDialog( "あなたの名前を入力してください:")JOptionPaneを使用してデータを非表示にする

ユーザーが名前を付けているときに文字を非表示にしたい。私はアプリケーションの途中でした。私がそれらの文字を隠すことができれば、私の仕事は終わる。私はそのラベル(JPasswordField)を保持するフォームが必要なので、JPasswordFieldを使用したくありません。

+0

ユーザーが入力した文字は、テキストフィールドに表示されません。それは空白のままですか? – Adam

+0

ユーザーを混乱させないように注意してください。入力を開始すると、ユーザーは何らかのフィードバックを期待します。フィードバックが表示されないボックスを使用すると、混乱する可能性があります。 – BlueFish

答えて

3
public static String getName() { 
    JPasswordField jpf = new JPasswordField(24); 
    JLabel jl = new JLabel("Enter Your Name: "); 
    Box box = Box.createHorizontalBox(); 
    box.add(jl); 
    box.add(jpf); 
    int x = JOptionPane.showConfirmDialog(null, box, "Name Entry", JOptionPane.OK_CANCEL_OPTION); 

    if (x == JOptionPane.OK_OPTION) { 
     return jpf.getText(); 
    } 
    return null; 
    } 
+0

:Boxについて詳しく教えてください... – user10101

+0

http://docs.oracle.com/javase/6/docs/api/javax/swing/Box.htmlを参照してください。 –

1

JPasswordFieldを使用できます。JPasswordFieldは、デフォルトで '*'で文字を置き換えます。

Why does JPasswordField.getPassword() create a String with the password in it?

そして、あなたはのJPasswordFieldに代わるものを探しているなら、あなたはこの読むことをお勧めします:

Is there an alternative to JPasswordField?

+0

JPasswordFieldを使用すると、フォームを作成することができます。そのようにしたくない場合は、JOPtionペインを使用する方法がありますか? – user10101

+0

@ user10101 'JOptionPane'は' Component'sを表示することができるので、パスワードフィールドを使うために 'JFrame'や' JDialog'( "フォーム"という意味です)を作成する必要はありません。 – lhballoti

+0

正確には、JOptionPaneはComponentを表示できますが、JOptionペインに入力しているデータをどのように非表示にするのですか? – user10101

1

あなたはこれを読むことをお勧めします

JOptionPaneを使用して、ユーザーとして空白を表示するJPasswordFieldを表示するソリューションです タイプ。

コードに入れたいのは、showPasswordPrompt()メソッドですが、コードにはmain()メソッドが含まれているため、ダイアログの外観を簡単にテストできます。

import java.awt.Component; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JPasswordField; 

public class JOptionPaneTest 
{ 
    public static String showPasswordPrompt(Component parent, String title) 
    { 
     // create a new JPasswordField 
     JPasswordField passwordField = new JPasswordField(); 
     // display nothing as the user types 
     passwordField.setEchoChar(' '); 
     // set the width of the field to allow space for 20 characters 
     passwordField.setColumns(20); 

     int returnVal = JOptionPane.showConfirmDialog(parent, passwordField, title, JOptionPane.OK_CANCEL_OPTION); 

     if (returnVal == JOptionPane.OK_OPTION) 
     { 
      // there's a reason getPassword() returns a char[], but we ignore this for now... 
      // see: http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords 
      return new String(passwordField.getPassword()); 
     } 
     else 
     { 
      return null; 
     } 
    } 

    public static void main(String[] args) 
    { 
     final JFrame frame = new JFrame(); 
     final JButton button = new JButton("Push Me For Dialog Box"); 

     button.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       String password = showPasswordPrompt(frame, "Enter Password:"); 

       button.setText(password); 
      } 
     }); 

     frame.add(button); 
     frame.setSize(400, 400); 
     frame.setVisible(true); 
    } 
} 
関連する問題