0

I want to make placeholder in JTextField and JPasswordField. To make it I use total 4 textfields (3 textfields named: tuser, tuser1, passfield and 1 passwordfield named: password).

I added two textfields (tuser and tuser1) together to making a placeholder and I again add textfield with passwordfield (passfield and password) to making placeholder. But I can't take input in the passowrdfield. My code is given below.

This is the code of placeholder for username which works properly.

tuser = new JTextField();
    tuser.setBorder(new MatteBorder(0, 0, 4, 0, (Color) Color.GREEN));
    tuser.setOpaque(false);
    tuser.setFont(new Font("Tahoma", Font.PLAIN, 15));
    tuser.setForeground(Color.WHITE);
    tuser.setCaretColor(Color.WHITE);
    tuser.addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent arg0) {
            if(arg0.getKeyChar()!=KeyEvent.VK_BACK_SPACE) {
                tuser1.setVisible(false);
                tuser1.setEnabled(false);
            }
            else if(tuser.getText().equals("")) {
                tuser1.setVisible(true);
                tuser1.setEnabled(false);
            }
        }
    });
    tuser.setBounds(79, 148, 343, 35);
    contentPane.add(tuser);
    tuser.setColumns(10);
    
    tuser1 = new JTextField();
    tuser1.setEditable(false);
    tuser1.setFont(new Font("Tahoma", Font.PLAIN, 15));
    tuser1.setForeground(Color.GRAY);
    tuser1.setText("User Name");
    tuser1.setEnabled(false);
    tuser1.setBorder(null);
    tuser1.setOpaque(false);
    tuser1.setBounds(79, 148, 343, 35);
    contentPane.add(tuser1);
    tuser1.setColumns(10);

And the same as above code I have used to make the passwordfield which doesn't work and I can't take input.This is the problem part. The code is below:

passfield = new JTextField();
    passfield.setBorder(null);
    passfield.setOpaque(false);
    passfield.setEnabled(false);
    passfield.setEditable(false);
    passfield.setFont(new Font("Tahoma", Font.PLAIN, 15));
    passfield.setForeground(Color.GRAY);
    passfield.setText("Password");
    passfield.setBounds(79, 213, 343, 35);
    contentPane.add(passfield);
    passfield.setColumns(10);
    
    password = new JPasswordField();
    password.setColumns(10);
    password.addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent arg0) {
            if(arg0.getKeyChar()!=KeyEvent.VK_BACK_SPACE) {
                passfield.setVisible(false);
                passfield.setEnabled(false);
            }
            else if(password.getText().equals("")) {
                passfield.setVisible(true);
                passfield.setEnabled(false);
            }
        }
    });
    password.setBorder(new MatteBorder(0, 0, 4, 0, (Color) Color.GREEN));
    password.setOpaque(false);
    password.setForeground(Color.WHITE);
    password.setFont(new Font("Tahoma", Font.PLAIN, 12));
    password.setBounds(79, 213, 343, 35);
    contentPane.add(password);

This password filed doesn't take any input. it's fully disabled.

MK.Whridoy
  • 68
  • 1
  • 8
  • I think No. because I use that solution.and I think that used only one textfield.But here I used 2 fields to make one placeholder. – MK.Whridoy Apr 11 '21 at 12:01
  • In one example you add the input field first, then the placeholder. In the other example you add the placeholder first, than the input field. Is that intentional? Also you might want to rename your variables to make it clear what the placeholder is and what the input is. – Progman Apr 11 '21 at 12:19
  • Thanks brother My problem is solved. This is the silly mistake I have done here.I put placeholder first that's why it's disabled. But cant you please tell me why it's happened?Does It effect to add anything first or last? – MK.Whridoy Apr 11 '21 at 12:24
  • You are placing objects on top of each other (which you shouldn't do in the first place), but your mouse click or interaction will hit only one of them or only on of them will react to it. – Progman Apr 11 '21 at 12:27

0 Answers0