I would like to add a hint value to my javax.swing.JTextField
. It should look like Firefox rendering of <input type="text" title="bla">
. This creates an edit field with the text 'bla' in the background. If the textbox has focus the title-text disappears and just reappears if the user leaves the editbox without text.
Is there a (free) swing component that does something like this?
addActionListener( action ); Now the event is fired when the Enter key is used. Also, an added benefit is that you can share the listener with a button even if you don't want to make the button a default button. JButton button = new JButton("Do Something"); button.
The class JTextField is a component that allows editing of a single line of text.
The main difference between JTextField and JTextArea in Java is that a JTextField allows entering a single line of text in a GUI application while the JTextArea allows entering multiple lines of text in a GUI application.
By default, a JTextField can allow numbers, characters, and special characters. Validating user input that is typed into a JTextField can be difficult, especially if the input string must be converted to a numeric value such as an int. In the below example, JTextField only allows entering numeric values.
You could create your own:
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import javax.swing.*; public class Main { public static void main(String[] args) { final JFrame frame = new JFrame(); frame.setLayout(new BorderLayout()); final JTextField textFieldA = new HintTextField("A hint here"); final JTextField textFieldB = new HintTextField("Another hint here"); frame.add(textFieldA, BorderLayout.NORTH); frame.add(textFieldB, BorderLayout.CENTER); JButton btnGetText = new JButton("Get text"); btnGetText.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String message = String.format("textFieldA='%s', textFieldB='%s'", textFieldA.getText(), textFieldB.getText()); JOptionPane.showMessageDialog(frame, message); } }); frame.add(btnGetText, BorderLayout.SOUTH); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setVisible(true); frame.pack(); } } class HintTextField extends JTextField implements FocusListener { private final String hint; private boolean showingHint; public HintTextField(final String hint) { super(hint); this.hint = hint; this.showingHint = true; super.addFocusListener(this); } @Override public void focusGained(FocusEvent e) { if(this.getText().isEmpty()) { super.setText(""); showingHint = false; } } @Override public void focusLost(FocusEvent e) { if(this.getText().isEmpty()) { super.setText(hint); showingHint = true; } } @Override public String getText() { return showingHint ? "" : super.getText(); } }
If you're still on Java 1.5, replace the this.getText().isEmpty()
with this.getText().length() == 0
.
Here is a simple way that looks good in any L&F:
public class HintTextField extends JTextField { public HintTextField(String hint) { _hint = hint; } @Override public void paint(Graphics g) { super.paint(g); if (getText().length() == 0) { int h = getHeight(); ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON); Insets ins = getInsets(); FontMetrics fm = g.getFontMetrics(); int c0 = getBackground().getRGB(); int c1 = getForeground().getRGB(); int m = 0xfefefefe; int c2 = ((c0 & m) >>> 1) + ((c1 & m) >>> 1); g.setColor(new Color(c2, true)); g.drawString(_hint, ins.left, h / 2 + fm.getAscent() / 2 - 2); } } private final String _hint; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With