diff options
author | Tom Kazimiers <tom@voodoo-arts.net> | 2013-12-11 23:53:29 -0500 |
---|---|---|
committer | Ingo Bauersachs <ingo@jitsi.org> | 2016-05-17 22:25:15 +0200 |
commit | 14a19e430d6c37bca2926ae3d1325a86e3d9d6f4 (patch) | |
tree | 7482a59e6b27eb1bde0947567908b56c0a974974 | |
parent | 8e3ceecef06ab8b8f04946465353096efb954207 (diff) | |
download | jitsi-14a19e430d6c37bca2926ae3d1325a86e3d9d6f4.zip jitsi-14a19e430d6c37bca2926ae3d1325a86e3d9d6f4.tar.gz jitsi-14a19e430d6c37bca2926ae3d1325a86e3d9d6f4.tar.bz2 |
Add contact dialog: support prompts in text fields
This commit adds a function which makes it possible to add faint gray
prompt to a text field. The foreground color of the prompt's text is the
one of the text field, but half transparent. As soon as text is added to
the text field, the prompt will vanish and it will re-appear when the
text field is emptied again.
-rw-r--r-- | src/net/java/sip/communicator/impl/gui/main/contactlist/AddContactDialog.java | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/net/java/sip/communicator/impl/gui/main/contactlist/AddContactDialog.java b/src/net/java/sip/communicator/impl/gui/main/contactlist/AddContactDialog.java index ffb93ea..950bb66 100644 --- a/src/net/java/sip/communicator/impl/gui/main/contactlist/AddContactDialog.java +++ b/src/net/java/sip/communicator/impl/gui/main/contactlist/AddContactDialog.java @@ -24,7 +24,9 @@ import java.util.*; import java.util.List; import javax.swing.*; +import javax.swing.border.*; import javax.swing.event.*; +import javax.swing.text.*; import org.jitsi.util.*; @@ -166,6 +168,47 @@ public class AddContactDialog } /** + * Adds a faint gray prompt to the provided text field that + * will vanish as soon as text is entered into the field. + */ + private void addPrompt(JTextField field, String text) + { + final JLabel prompt = new JLabel(text); + + // Give prompt a foreground color like the original + // text field, but with half transparency. + final Color fg = field.getForeground(); + final Color color = new Color( + fg.getRed(), fg.getGreen(), fg.getBlue(), 128); + + // Mimic properties of given text field + prompt.setFont(field.getFont()); + prompt.setForeground(color); + prompt.setBorder(new EmptyBorder(field.getInsets())); + prompt.setHorizontalAlignment(JLabel.LEADING); + + // Add handler to hide prompt when text is entered + final Document doc = field.getDocument(); + doc.addDocumentListener( new DocumentListener() { + public void insertUpdate(DocumentEvent e) + { + prompt.setVisible(doc.getLength() == 0); + } + + public void removeUpdate(DocumentEvent e) + { + prompt.setVisible(doc.getLength() == 0); + } + + public void changedUpdate(DocumentEvent e) {} + }); + + // Add prompt to text field + field.setLayout( new BorderLayout() ); + field.add(prompt); + } + + /** * Initializes the dialog. */ private void init() |