如何在JTextfield中设置像Placeholder一样的文本

时间:2014-03-14 04:42:27

标签: java swing jtextfield prompt windowbuilder

我希望在表单加载时将一些文本放在text-Field中,指示用户,当用户点击该文本时,文本会自动删除。

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

我已经编写了上面的代码,但是当用户点击我要删除它的文本按钮时,它会显示文本并保持不变。

有没有办法完成这项任务?

3 个答案:

答案 0 :(得分:13)

我用来覆盖文本字段绘制方法,直到我最终得到更多自定义文本字段然后我真的想...

然后我发现this prompt API易于使用,并且不要求您扩展任何组件。它还有一个很好的"好友" API

这已经被包含在SwingLabs SwingX library中,这使得它甚至可以使用......

例如(这使用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);
            }
        });
    }

}

答案 1 :(得分:0)

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));  
        }  

    }  
});

答案 2 :(得分:-3)

您可以使用download this NetBeans plugin来创建只有一行的占位符。