Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7925393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:16:32+00:00 2026-06-03T18:16:32+00:00

I don’t know if I got the right name for it, but I’m looking

  • 0

I don’t know if I got the right name for it, but I’m looking to see if there is a specific way to implement a text field so that while it doesn’t have focus and is empty, a faint gray string of text is displayed in the field. When the field is clicked, the text should go away, exactly like how the Search bar like that of StackOverflow works. I know that I can change use setForeground() and focus listeners to accomplish this, but I was just wondering if anyone knew of some Java implementation that could handle this for me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T18:16:33+00:00Added an answer on June 3, 2026 at 6:16 pm

    For what it’s worth, I found it interesting to actually implement it, so I thought to share it with you (I am not looking for votes).

    It’s really non-invasive since all you have to do is call new GhostText(textField, "Please enter some text here...");. The rest of the code is only to make it run.

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    
    public class Test {
    
        public static class GhostText implements FocusListener, DocumentListener, PropertyChangeListener {
            private final JTextField textfield;
            private boolean isEmpty;
            private Color ghostColor;
            private Color foregroundColor;
            private final String ghostText;
    
            protected GhostText(final JTextField textfield, String ghostText) {
                super();
                this.textfield = textfield;
                this.ghostText = ghostText;
                this.ghostColor = Color.LIGHT_GRAY;
                textfield.addFocusListener(this);
                registerListeners();
                updateState();
                if (!this.textfield.hasFocus()) {
                    focusLost(null);
                }
            }
    
            public void delete() {
                unregisterListeners();
                textfield.removeFocusListener(this);
            }
    
            private void registerListeners() {
                textfield.getDocument().addDocumentListener(this);
                textfield.addPropertyChangeListener("foreground", this);
            }
    
            private void unregisterListeners() {
                textfield.getDocument().removeDocumentListener(this);
                textfield.removePropertyChangeListener("foreground", this);
            }
    
            public Color getGhostColor() {
                return ghostColor;
            }
    
            public void setGhostColor(Color ghostColor) {
                this.ghostColor = ghostColor;
            }
    
            private void updateState() {
                isEmpty = textfield.getText().length() == 0;
                foregroundColor = textfield.getForeground();
            }
    
            @Override
            public void focusGained(FocusEvent e) {
                if (isEmpty) {
                    unregisterListeners();
                    try {
                        textfield.setText("");
                        textfield.setForeground(foregroundColor);
                    } finally {
                        registerListeners();
                    }
                }
    
            }
    
            @Override
            public void focusLost(FocusEvent e) {
                if (isEmpty) {
                    unregisterListeners();
                    try {
                        textfield.setText(ghostText);
                        textfield.setForeground(ghostColor);
                    } finally {
                        registerListeners();
                    }
                }
            }
    
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                updateState();
            }
    
            @Override
            public void changedUpdate(DocumentEvent e) {
                updateState();
            }
    
            @Override
            public void insertUpdate(DocumentEvent e) {
                updateState();
            }
    
            @Override
            public void removeUpdate(DocumentEvent e) {
                updateState();
            }
    
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    init();
                }
            });
        }
    
        public static void init() {
            JFrame frame = new JFrame("Test ghost text");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new JPanel();
            JTextField textField = new JTextField();
            JButton button = new JButton("Grab focus");
            GhostText ghostText = new GhostText(textField, "Please enter some text here...");
            textField.setPreferredSize(new Dimension(300, 24));
            panel.add(textField);
            panel.add(button);
            frame.add(panel);
            frame.pack();
            frame.setVisible(true);
            button.grabFocus();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know how to google for such, but is there a way to query
Don't know if I worded the question right, but basically what I want to
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this is the right place to ask this, but I will
Don't ask why, but is there any way to suppress a failed linking error?
Don't know if this is an eclipse specific problem but whenever I declare a
Don't ask how I got there, but I was playing around with some masking,
Don't know if there is a better way to do this, so that is
Don't really know how to formulate the title, but it should be pretty obvious
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.