-1

I don't know what's wrong with my code but the password does not turn into dots. Please help me. >.<

Here is my UI code:

private final JPanel contentPanel = new JPanel();
public JPasswordField password_id;
public JFormattedTextField user_ID;
public JButton btnAccept;
private JFormattedTextField previouslyFocusedTextBox = user_ID;
public login(final JFrame parent, boolean modal) {
    super(parent, modal);

    setBounds(100, 100, 469, 428);
    getContentPane().setLayout(new BorderLayout());
    final Toolkit toolkit = Toolkit.getDefaultToolkit();
    final Dimension screenSize = toolkit.getScreenSize();
    final int x = (screenSize.width - getWidth()) / 2;
    final int y = (screenSize.height - getHeight()) / 2;
    setLocation(x, y);
    setResizable(false);

    FlowLayout flow_2 = new FlowLayout(0,0,0);
    final JPanel panel_1 = new JPanel();

    panel_1.setBackground(new Color(128, 0, 128));
    panel_1.setPreferredSize(new Dimension(10, 90));
    panel_1.setBounds(new Rectangle(0, 0, 100, 100));
    panel_1.setBorder(new EmptyBorder(50, 90, 0, 0));
    panel_1.setLayout(flow_2);
    header_Login.add(panel_1, BorderLayout.SOUTH);

    user_ID = new JFormattedTextField();
    user_ID.setFocusTraversalKeysEnabled(true);
    user_ID.setFocusable(true);
    user_ID.addFocusListener(this);
    user_ID.setBounds(0, 0, 423, 45);
    panel_1.add(user_ID);
    user_ID.setMinimumSize(new Dimension(8, 32));
    user_ID.setUI(new JTextFieldHintUI("Enter ID Here", Color.gray));
    user_ID.setHorizontalAlignment(SwingConstants.LEFT);
    user_ID.setFont(new Font("Calibri", Font.BOLD, 35));
    user_ID.setPreferredSize( new Dimension(10, 40) );
    user_ID.setColumns(20);

    FlowLayout flow_3 = new FlowLayout(0,0,0);
    JPanel body_Login = new JPanel();
    body_Login.setBackground(new Color(128, 0, 128));
    body_Login.setBorder(new EmptyBorder(0, 0, 0, 0));
    getContentPane().add(body_Login, BorderLayout.CENTER);
    body_Login.setLayout(flow_3);

    password_id = new JPasswordField ();
    password_id.setFocusTraversalKeysEnabled(true);
    password_id.setFocusable(true);
    password_id.addFocusListener(this);
    body_Login.add(password_id);
    password_id.setMinimumSize(new Dimension(8, 32));
    password_id.setUI(new JTextFieldHintUI("Enter PASSWORD Here", Color.gray));
    password_id.setHorizontalAlignment(SwingConstants.LEFT);
    password_id.setFont(new Font("Calibri", Font.BOLD, 35));
    password_id.setPreferredSize( new Dimension(100, 40) );
    password_id.setColumns(20);

And at the end of the code I got this:

   public void focusGained(FocusEvent ev) {
    //System.out.println("CHANGE");
    if(ev.getSource() instanceof JFormattedTextField) {
        previouslyFocusedTextBox = (JFormattedTextField) ev.getSource();
        }
}

public void focusLost(FocusEvent arg0) {
    // TODO Auto-generated method stub

}

I know its a bit messy and I'm still new in programming the UI manually so that's why I don't know my way around. Anyway, my JPasswordField is does not turn the text into dots. I already made it work a while ago but I revert it back again thinking it wasn't working. Anyway, I can't make it work again and I really don't know what I will do to make it work. The original code for the password is:

   public JFormattedTextField password_id;

   password_id = new JFormattedTextField((Format) null);

When I changed my code into JPasswordField the field does not focus on the password field maybe because of the FormattedTextField in previouslyFocusedTextBox. I really don't know what I should do to make the JPasswordField to make work. This project was not really made by me that's why I don't understand some of its uses. Please help me out.

user3771102
  • 558
  • 2
  • 8
  • 27
  • I think you have to call the method `setEchoChar(char c)` on password_id – Jens Jun 24 '14 at 13:21
  • 1) `setBounds(100, 100, 469, 428);` should be `pack()` but it needs to be called after the components are added. 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 3) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Jun 24 '14 at 13:27
  • @Jens Well even if I put that still it doesn't work. – user3771102 Jun 24 '14 at 13:30
  • @AndrewThompson Ohhh I see. Thank you for that but since I wasn't the one who did this I don't know why did he put those. And since I'm new to this I don't know how to post MCVE. I just want to make the password hidden. – user3771102 Jun 24 '14 at 13:33
  • have you tied setting a echo character ? [`setEchoChar`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JPasswordField.html#setEchoChar(char)) – Dawnkeeper Jun 24 '14 at 13:40
  • @user3771102 Have you tried to remove the `JTextFieldHintUI`? Maybe thats the problem. – Jens Jun 24 '14 at 13:42
  • @Jens THANK YOU!! I FOUND IT! – user3771102 Jun 24 '14 at 14:11
  • *"since I'm new to this I don't know how to post MCVE"* That's why I linked to an explanation. What don't you understand? – Andrew Thompson Jun 24 '14 at 14:12
  • I'm sorry @AndrewThompson I'm not as good as any of you. I'm good at editing codes but not creating them from scratch. Java was really not my top skill and I only know some basics of Java. I was really not cut out for this that's why I do ask for help. Forgive me if I don't know the things that you know. And by the way, Thank you for your immediate reply and for helping me correct my codes. – user3771102 Jun 24 '14 at 14:29

1 Answers1

0

Use JPaswordField instead of JTextFormated Field

dawidklos
  • 902
  • 1
  • 9
  • 32