public static DefaultOverlayable createOverlayTextArea(final JTextArea area, String mess) {
// build
JLabel overlayLabel = new JLabel(mess);
overlayLabel.setForeground(SystemColor.textInactiveText);
final DefaultOverlayable overlayTextArea = new DefaultOverlayable(new JScrollPane(area));
area.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
if (area.getDocument().getLength() > 0) {
overlayTextArea.setOverlayVisible(false);
}
}
@Override
public void removeUpdate(DocumentEvent e) {
if (area.getDocument().getLength() == 0) {
overlayTextArea.setOverlayVisible(true);
}
}
@Override
public void changedUpdate(DocumentEvent e) {
}
});
area.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
overlayTextArea.setOverlayVisible(false);
}
@Override
public void focusLost(FocusEvent e) {
overlayTextArea.setOverlayVisible(area.getDocument().getLength() == 0);
}
});
overlayTextArea.addOverlayComponent(overlayLabel);
// has text?
overlayTextArea.setOverlayVisible(area.getDocument().getLength() == 0);
// get it
return overlayTextArea;
}