How to set Text like Placeholder in JTextfield in swing
Asked Answered
M

3

6

I want to put some texts in text-Field when the form is load which instruct to user and when user click on that text-filed the texts remove automatically.

 txtEmailId = new JTextField();
 txtEmailId.setText("Email ID");

i have wrote above code but it display the text and keep as it is when user click on that text button i want to remove it.

is there any way to do this task?

Midwinter answered 14/3, 2014 at 4:42 Comment(1)
Take a look here: #1739466Outmost
A
14

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...

For example (this uses SwingX-1.6.4)

PromptSupport

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdesktop.swingx.prompt.PromptSupport;

public class PromptExample {

    public static void main(String[] args) {
        new PromptExample();
    }

    public PromptExample() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTextField bunnies = new JTextField(10);
                JTextField ponnies = new JTextField(10);
                JTextField unicorns = new JTextField(10);
                JTextField fairies = new JTextField(10);

                PromptSupport.setPrompt("Bunnies", bunnies);
                PromptSupport.setPrompt("Ponnies", ponnies);
                PromptSupport.setPrompt("Unicorns", unicorns);
                PromptSupport.setPrompt("Fairies", fairies);

                PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT, bunnies);
                PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIGHLIGHT_PROMPT, ponnies);
                PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.SHOW_PROMPT, unicorns);

                PromptSupport.setFontStyle(Font.BOLD, bunnies);
                PromptSupport.setFontStyle(Font.ITALIC, ponnies);
                PromptSupport.setFontStyle(Font.ITALIC | Font.BOLD, unicorns);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                frame.add(bunnies, gbc);
                frame.add(ponnies, gbc);
                frame.add(unicorns, gbc);
                frame.add(fairies, gbc);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}
Area answered 14/3, 2014 at 4:44 Comment(3)
From Swing-X Libaries as linked in the answer...Area
it seems SwingX library Project has been closed.Benzel
@Razi Well, Swing is kind of dated and has seen any love for awhile, so it's not surprising. You could probably still pick up the source code if you needed itArea
G
1
JTextField busqueda = new JTextField(20);  

add(busqueda);   
busqueda.setHorizontalAlignment(SwingConstants.CENTER);  

if (busqueda.getText().length() == 0) {  
    busqueda.setText("Buscar");  
    busqueda.setForeground(new Color(150, 150, 150));  
}  

busqueda.addFocusListener(new FocusListener() {  

    @Override  
    public void focusGained(FocusEvent e) {  
        busqueda.setText("");  
        busqueda.setForeground(new Color(50, 50, 50));  
    }  

    @Override  
    public void focusLost(FocusEvent e) { 

        if (busqueda.getText().length() == 0) {  
            busqueda.setText("Buscar");  
            busqueda.setForeground(new Color(150, 150, 150));  
        }  

    }  
});
Ginglymus answered 15/7, 2016 at 1:45 Comment(1)
Adding an explanation to how this code answers the question will help future visitors.Hagberry
B
-3

You can download this NetBeans plugin which you can use to create a placeholder with just one line.

Balch answered 23/1, 2017 at 22:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.