Fantom-text in jTextField. How?

时间秒杀一切 提交于 2019-12-14 03:07:29

问题


We come across some popular sites login screen, where a faint gray Username is written in username textbox. I wonder can be this implemented in java swings?

I have theories, implementing by mouseEvents. Couldn't figure out how to to.


回答1:


Check out Text Prompt.

It allows you to display text and/or icon as a prompt for the usage of the text field. The prompt will be removed as soon as any characters are entered into the text field.

You can also control when the prompt is displayed:

  1. ALWAYS – the prompt is displayed whether the text field has focus or not.
  2. FOCUS_GAINED – the prompt is displayed when the text field gains focus (and is hidden when focus is lost)
  3. FOCUS_LOST – the prompt is displayed when the text field loses focus (and is hidden when focus is gained)



回答2:


One way:

  • Create a class that gives the JTextField phantom text at start up
  • Set the text's foreground to a light color
  • Use a FocusListener, one that will select all the phantom text and that will change the foreground to the default color (set to null).

For example:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class FantomTextFieldTest extends JPanel {
    FantomTextField fantomTextField = new FantomTextField("Fantom Text");

    public FantomTextFieldTest() {
        setLayout(new GridLayout(1, 0, 5, 0));
        add(new JTextField(20));
        add(fantomTextField.getTextField());
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        FantomTextFieldTest mainPanel = new FantomTextFieldTest();
        JFrame frame = new JFrame("FantomTextFieldTest");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
}

class FantomTextField {
    private static final Color FANTOM_FG = new Color(150, 150, 150, 150);
    private JTextField textField;
    private String fantomText;
    private boolean showFantomText = true;

    public FantomTextField(String fantomText) {
        this.fantomText = fantomText;
        textField = new JTextField();
        textField.setText(fantomText);
        textField.setForeground(FANTOM_FG);
        textField.addFocusListener(new FtfFocusListener());
    }

    public FantomTextField(String fantomText, int columns) {
        this(fantomText);
        textField.setColumns(columns);
    }

    public JTextField getTextField() {
        return textField;
    }

    public String getFantomText() {
        return fantomText;
    }

    public void setFantomText(String fantomText) {
        this.fantomText = fantomText;
        textField.setText(fantomText);
        textField.setForeground(FANTOM_FG);
        showFantomText = true;
    }

    private class FtfFocusListener extends FocusAdapter {
        @Override
        public void focusGained(FocusEvent e) {
            if (showFantomText) {
                textField.selectAll();
                textField.setForeground(null);
                showFantomText = false;
            }
        }
    }       
}


来源:https://stackoverflow.com/questions/44615390/fantom-text-in-jtextfield-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!