Answers for "jtextfield placeholder"

1

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);
			}
		});
Posted by: Guest on December-30-2020

Browse Popular Code Answers by Language