Java Jtextfield with Input Hint

Java JTextField with input hint

Take a look at this one: http://code.google.com/p/xswingx/

It is not very difficult to implement it by yourself, btw. A couple of listeners and custom renderer and voila.

How to set Text like Placeholder in JTextfield in swing

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

}

Adding hint to TextField

Built-in feature

After some research I found that this is an integrated feature in all of the input controls (TextField, TextArea, DateField, ComboBox).

Vaadin Flow (Vaadin 10)

The feature is a property called Placeholder.

You can optionally pass the placeholder text to the constructor of TextField, along with optional initial value.

new TextField( "label goes here" , "hint goes here" ) 

Or call the setter and getter: TextField::setPlaceholder and TextField.getPlaceholder.

myTextField.setPlaceholder( "Hint goes here" ) ;

Vaadin 8

The feature is a property called Placeholder.

Call the getter/setter methods: TextField::getPlaceholder and TextField.setPlaceholder.

myTextField.setPlaceholder( "Hint goes here" ) ;

Vaadin 7

The feature is a property called InputPrompt.

Call the getter/setter methods: TextField::setInputPrompt and TextField::getInputPrompt.

myTextField.setInputPrompt("Hint goes here"); 

How to set different Limit in JTextField for showing Input Hint and Input

Okay, I'll check DocumentFilter in few min

You haven't changed your code? The DocumentFilter is the preferred approach because it is reusable. You can add it to any Document so it will work for a JTextField, JTextArea, JTextPane.

Both the requirements are working. But the problem is that if I set the limit to 6, then it sets the input length to 6. I need to set an additional limit.

You need to use a different approach. For example you can use the Text Prompt class. The prompt is independent of the actual text so the lengths can be different.

java swing JTextField set PlaceHolder

Try this class:

package playground;

import java.awt.*;

import javax.swing.*;
import javax.swing.text.Document;

@SuppressWarnings("serial")
public class PlaceholderTextField extends JTextField {

public static void main(final String[] args) {
final PlaceholderTextField tf = new PlaceholderTextField("");
tf.setColumns(20);
tf.setPlaceholder("All your base are belong to us!");
final Font f = tf.getFont();
tf.setFont(new Font(f.getName(), f.getStyle(), 30));
JOptionPane.showMessageDialog(null, tf);
}

private String placeholder;

public PlaceholderTextField() {
}

public PlaceholderTextField(
final Document pDoc,
final String pText,
final int pColumns)
{
super(pDoc, pText, pColumns);
}

public PlaceholderTextField(final int pColumns) {
super(pColumns);
}

public PlaceholderTextField(final String pText) {
super(pText);
}

public PlaceholderTextField(final String pText, final int pColumns) {
super(pText, pColumns);
}

public String getPlaceholder() {
return placeholder;
}

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

if (placeholder == null || placeholder.length() == 0 || getText().length() > 0) {
return;
}

final Graphics2D g = (Graphics2D) pG;
g.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(getDisabledTextColor());
g.drawString(placeholder, getInsets().left, pG.getFontMetrics()
.getMaxAscent() + getInsets().top);
}

public void setPlaceholder(final String s) {
placeholder = s;
}

}

Making a JTextField with Vanishing Text

You could use a FocusListener and when the JTextField receives focus, empty the text.

Of course you will want a state marker to indicate it has the default text and not do this once you have user entered text. Either that or after the FocusListener is hit the first time, remove it.

textField.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
JTextField source = (JTextField)e.getComponent();
source.setText("");
source.removeFocusListener(this);
}
});

Java: Add Place Holder on JTextField

Check out Text Prompt for a flexible solution.

You can control when prompt is displayed (always, focus gained or focus lost). You can also customize the style of the text.

How to add hint text in a Textfield in JavaFX

I do it like this:

userTextField.setPromptText("name"); //to set the hint text
userTextField.getParent().requestFocus(); //to not setting the focus on that node so that the hint will display immediately


Related Topics



Leave a reply



Submit