View Javadoc
1   /**
2    * Copyright (C) 2007 Joern Krueger surething@users.sourceforge.net
3    * 
4    * This program is free software; you can redistribute
5    * it and/or modify it under the terms of the GNU General
6    * Public License version 2 as published by the Free Software
7    * Foundation.
8    *
9    * This program is distributed in the hope that it will be
10   * useful, but WITHOUT ANY WARRANTY; without even the implied
11   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12   * PURPOSE.  See the GNU General Public License for more
13   * details.
14   *
15   * You should have received a copy of the GNU General Public
16   * License along with this program; if not, write to the
17   * Free Software Foundation, Inc., 59 Temple Place,
18   * Suite 330, Boston, MA  02111-1307  USA
19   */
20  package de.surething.lda.ui;
21  
22  import java.awt.Frame;
23  import java.awt.GridBagConstraints;
24  import java.awt.GridBagLayout;
25  import java.awt.Insets;
26  import java.awt.event.KeyAdapter;
27  import java.awt.event.KeyEvent;
28  
29  import javax.swing.JDialog;
30  import javax.swing.JPanel;
31  import javax.swing.JPasswordField;
32  import javax.swing.border.TitledBorder;
33  
34  import de.surething.lda.resources.Messages;
35  
36  @SuppressWarnings("serial")
37  public class PasswordDialog extends JDialog {
38  
39      private JPasswordField password;
40  
41      public PasswordDialog(Frame owner, String title) {
42          super(owner, title, true);
43  
44          createUI();
45          pack();
46          setLocationRelativeTo(owner);
47      }
48  
49      public void showDialog() {
50          setVisible(true);
51      }
52  
53      public void createUI() {
54          JPanel panel = new JPanel();
55  
56          panel.setLayout(new GridBagLayout());
57          GridBagConstraints c = new GridBagConstraints();
58          c.insets = new Insets(5, 5, 5, 5);
59          c.fill = GridBagConstraints.HORIZONTAL;
60          c.gridy = 0;
61          c.gridx = 0;
62          c.weighty = 0.0;
63          c.weightx = 1.0;
64          c.anchor = GridBagConstraints.WEST;
65  
66          panel.setBorder(new TitledBorder(Messages.getString("PasswordDialog.BorderTitle")));
67          password = new JPasswordField(25);
68          password.addKeyListener(new KeyAdapter() {
69              @Override
70              public void keyReleased(KeyEvent e) {
71                  if (e.getKeyCode() == KeyEvent.VK_ENTER) {
72                      dispose();
73                  }
74              }
75          });
76  
77          panel.add(password, c);
78          getContentPane().add(panel);
79      }
80  
81      public char[] getPassword() {
82          return password.getPassword();
83      }
84  }