Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could create your own:</p> <pre><code>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(); } } </code></pre> <p>If you're still on Java 1.5, replace the <code>this.getText().isEmpty()</code> with <code>this.getText().length() == 0</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload