java jtextfield placeholder text code example
Example 1: css style placeholder
input::-webkit-input-placeholder {
}
input::-ms-input-placeholder {
}
input:-ms-input-placeholder {
}
input::-moz-placeholder {
opacity: 1;
}
input:-moz-placeholder {
opacity: 1;
}
input::placeholder {
}
Example 2: jtextfield placeholder
txtField = new JTextField();
txtField.setForeground(Color.LIGHT_GRAY);
txtField.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
if(txtField.getText().trim().equals("Hello World")) {
txtField.setText("");
}
txtField.setForeground(Color.BLACK);
}
@Override
public void focusLost(FocusEvent e) {
if(txtField.getText().trim().equals("")) {
txtField.setText("Hello World");
}
txtField.setForeground(Color.LIGHT_GRAY);
}
});