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.