开发者

Example text in JTextField

开发者 https://www.devze.com 2023-02-11 03:36 出处:网络
I am looking for a way to put example text into a swing JTextField and have it grayed out. The example text should then disappear as soon as any thing is entered into that text field. Some what simila

I am looking for a way to put example text into a swing JTextField and have it grayed out. The example text should then disappear as soon as any thing is entered into that text field. Some what similar to what stackoverflow does when a user is posting a question with the title field.

I would like it if it was already a extended implementation of JTextField so that I can just drop it in as a simple replacement. Anything from swingx would work. I guess if there is not an easy way to do this my option will probably be to override the paint method of JText开发者_运维百科Field do something that way maybe.

Thanks


The Text Prompt class provides the required functionality without using a custom JTextField.

It allows you to specify a prompt that is displayed when the text field is empty. As soon as you type text the prompt is removed.

The prompt is actually a JLabel so you can customize the font, style, colour, transparency etc..:

JTextField tf7 = new JTextField(10);
TextPrompt tp7 = new TextPrompt("First Name", tf7);
tp7.setForeground( Color.RED );

Some examples of customizing the look of the prompt:

Example text in JTextField


If you can use external librairies, the Swing components from Jide software have what you are looking for; it's called LabeledTextField (javadoc) and it's part of the JIDE Common Layer (Open Source Project) - which is free. It's doing what mklhmnn suggested.


How about initialize the text field with default text and give it a focus listener such that when focus is gained, if the text .equals the default text, call selectAll() on the JTextField.


Rather than overriding, put a value in the field and add a KeyListener that would remove the value when a key stroke is registered. Maybe also have it change the foreground.

You could wrap this up into your own custom JTextField class that would take the default text in a constructor.


private JLabel l;

JPromptTextField(String prompt) {
    l = new JLabel(prompt, SwingConstants.CENTER);
    l.setForeground(Color.GRAY);
}

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

    if (this.getText().length() == 0) {
        // Reshape the label if needed, then paint

        final Rectangle mine = this.getBounds();
        final Rectangle its = l.getBounds(); 
        boolean resized = (mine.width != its.width) || (mine.height != its.height);
        boolean moved = (mine.x != its.x) || (mine.y != its.y);
        if (resized || moved)
            l.setBounds(mine);

        l.paint(g);
    }
}


You can't do that with a plain text field, but you can put a disabled JLabel on top of the JTextField and hide it if the text field gets the focus.


Do it like this:

  1. Define the string with the initial text you like and set up your TextField:

    String initialText = "Enter your initial text here";
    jTextField1.setText(initialText);
    
  2. Add a Focus Listener to your TextField, which selects the entire contents of the TextField if it still has the initial value. Anything you may type in will replace the entire contents, since it is selected.

    jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusGained(java.awt.event.FocusEvent evt) {
           if (jTextField1.getText().equals(initialText)) {
              jTextField1.selectAll();
           }
        }
    });
    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号