Java - placeholder on textfield

26,019

Solution 1

I found this on the oracle forums.

public class TextFieldWithPrompt extends JTextField{

@Override
protected void paintComponent(java.awt.Graphics g) {
    super.paintComponent(g);

    if(getText().isEmpty() && ! (FocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == this)){
        Graphics2D g2 = (Graphics2D)g.create();
        g2.setBackground(Color.gray);
        g2.setFont(getFont().deriveFont(Font.ITALIC));
        g2.drawString("zip", 5, 10); //figure out x, y from font's FontMetrics and size of component.
        g2.dispose();
    }
  }

https://forums.oracle.com/forums/thread.jspa?threadID=1349874

Solution 2

I use to override the text fields paint method, until I ended up with more custom text fields then I really wanted...

Then I found this prompt API which is simple to use and doesn't require you to extend any components. It also has a nice "buddy" API

This has now been included in the SwingLabs, SwingX library which makes it even eaiser to use...

Solution 3

Try this.

private void txtUserNameFocusGained(java.awt.event.FocusEvent evt) {                                        
    String username = txtUserName.getText();
    if(username.equals("User Name")){
        txtUserName.setCaretPosition(0);
    }

}                                       

private void txtUserNameFocusLost(java.awt.event.FocusEvent evt) {                                      
    String username = txtUserName.getText();
    if(username.equals("")){
        txtUserName.setForeground(new java.awt.Color(86, 86, 86));
        txtUserName.setCaretPosition(0);
        txtUserName.setText("User Name");
    }
}                                     

private void txtUserNameKeyPressed(java.awt.event.KeyEvent evt) {                                       
    String username = txtUserName.getText();
    if(username.equals("User Name")){
        txtUserName.setForeground(new java.awt.Color(0, 0, 0));
        txtUserName.setText(null);
        txtUserName.setCaretPosition(0);
    }
}

Be adviced, the name of the text field is "txtUserName". You can see the output as like this.

image description here

Share:
26,019
Jordashiro
Author by

Jordashiro

Currently a student.

Updated on April 09, 2020

Comments

  • Jordashiro
    Jordashiro about 4 years

    I'm trying to create a GUI with Swing. My problem is, I have a textfield, but I want it to have a "placeholder" (like in html). I read here and there that it can be done by overriding the paint() of the textfield.

    Since my code is generated I found out that I need to use the "Custom Creation Code" to override the code that was generated.

    Here is what I have put in the "Custom Creation Code" field

    new javax.swing.JTextField()
    {
        String test = super.getText();
        String hint = "Username";
    
        public void paint(Graphics g)
        {
            if ( test == null || test.length() < 1 ) {
                g.setColor( Color.red );
                g.drawString(hint, 0, 0);
            }
    
            g.setColor(Color.BLACK);
            super.paint(g);
        }
    }
    

    This generates the following output

    javax.swing.JTextField username = new javax.swing.JTextField()
    {
        String test = super.getText();
        String hint = "Username";
    
        public void paint(Graphics g)
        {
            if ( test == null || test.length() < 1 ) {
                g.setColor( Color.red );
                g.drawString(hint, 0, 0);
            }
    
            g.setColor(Color.BLACK);
            super.paint(g);
        }
    };
    

    For now I see the textField but there is nothing in it, maybe I need to add some function onto some event, but I am not sure.

    I would be grateful if anyone could lend a hand.

    EDIT : Here is a demo of what I want to do : http://davidwalsh.name/demo/html5-placeholder.php

  • Jordashiro
    Jordashiro over 11 years
    nice find , i would look into it
  • Jordashiro
    Jordashiro over 11 years
    The link you provided worked , maybe put another answer with this link so the solution is clear
  • MadProgrammer
    MadProgrammer over 11 years
    It's actually part of the latest SwingX library!
  • Olivier Grégoire
    Olivier Grégoire over 6 years
    What if my user name is "User Name"?
  • Panduka Nandara
    Panduka Nandara over 6 years
    There is no problem. It is considered as a name.