placeholder textarea code example

Example 1: textarea placeholder

<!-- Make sure that there is No space and No breaklines between the <textarea></textarea>. Example below: -->
<textarea placeholder="Enter Details..."></textarea>

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);
			}
		});

Tags:

Java Example