Adding a watermark to an empty JCombobox

I am trying to reproduce the behavior of the Firefox or Safari search field or the stackoverflow.com search field in the upper right corner of this page.

I mean, when the text of the edited JComboBox missing, the text of the instruction is displayed, for example, "Type here" or something else. Focusing the JComboBox text. If focus is lost without entering text, the instruction text is returned.

+4
source share
2 answers

Here is something simple I threw together. I'm sure you can remove it. Since the code runs on JTextField, you will need to get a combobox editor. I don't know anything about how glazed lists are implemented, so I just assume that this will work for you.

 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.event.*; import javax.swing.text.*; public class TextPrompt extends JLabel implements FocusListener, DocumentListener { private JTextComponent component; private Document document; public TextPrompt(String text, JTextComponent component) { this.component = component; document = component.getDocument(); setText( text ); setFont( component.getFont() ); setBorder( new EmptyBorder(component.getInsets()) ); component.addFocusListener( this ); document.addDocumentListener( this ); component.add( this ); } public void checkForPrompt() { if (document.getLength() == 0) setSize( component.getSize() ); else setSize(0, 0); } // Implement FocusListener public void focusGained(FocusEvent e) { checkForPrompt(); } public void focusLost(FocusEvent e) { setSize(0, 0); } // Implement DocumentListener public void insertUpdate(DocumentEvent e) { checkForPrompt(); } public void removeUpdate(DocumentEvent e) { checkForPrompt(); } public void changedUpdate(DocumentEvent e) {} public static void main(String[] args) { JPanel panel = new JPanel(); JTextField tf1 = new JTextField(10); panel.add(tf1); JTextField tf2 = new JTextField(10); panel.add(tf2); new TextPrompt("First Name", tf1); new TextPrompt("Last Name", tf2); JFrame frame = new JFrame(); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.add(panel); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible(true); } } 
+5
source

It is called waterMark . JQuery has one. I never tired of applying a watermark to jComboBox.
Good luck.

0
source

All Articles