No sé si obtuve el nombre correcto para ello, pero estoy buscando para ver si hay una manera específica de implementar un campo de texto para que, aunque no tenga el foco y esté vacío, un gris tenue cadena de texto se muestra en el campo. Cuando se hace clic en el campo, el texto debe desaparecer, exactamente como funciona la barra de búsqueda como la de StackOverflow. Sé que puedo cambiar el uso de setForeground()
y centrar a los oyentes para lograr esto, pero me preguntaba si alguien sabía de alguna implementación de Java que pudiera manejar esto por mí.¿Cómo se muestra el débil "texto fantasma" gris en un JTextField?
Respuesta
Por lo que vale, me pareció interesante implementarlo, así que pensé en compartirlo contigo (no estoy buscando votos).
Realmente no es invasivo ya que todo lo que tiene que hacer es llamar al new GhostText(textField, "Please enter some text here...");
. El resto del código es solo para hacer que se ejecute.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Test {
public static class GhostText implements FocusListener, DocumentListener, PropertyChangeListener {
private final JTextField textfield;
private boolean isEmpty;
private Color ghostColor;
private Color foregroundColor;
private final String ghostText;
protected GhostText(final JTextField textfield, String ghostText) {
super();
this.textfield = textfield;
this.ghostText = ghostText;
this.ghostColor = Color.LIGHT_GRAY;
textfield.addFocusListener(this);
registerListeners();
updateState();
if (!this.textfield.hasFocus()) {
focusLost(null);
}
}
public void delete() {
unregisterListeners();
textfield.removeFocusListener(this);
}
private void registerListeners() {
textfield.getDocument().addDocumentListener(this);
textfield.addPropertyChangeListener("foreground", this);
}
private void unregisterListeners() {
textfield.getDocument().removeDocumentListener(this);
textfield.removePropertyChangeListener("foreground", this);
}
public Color getGhostColor() {
return ghostColor;
}
public void setGhostColor(Color ghostColor) {
this.ghostColor = ghostColor;
}
private void updateState() {
isEmpty = textfield.getText().length() == 0;
foregroundColor = textfield.getForeground();
}
@Override
public void focusGained(FocusEvent e) {
if (isEmpty) {
unregisterListeners();
try {
textfield.setText("");
textfield.setForeground(foregroundColor);
} finally {
registerListeners();
}
}
}
@Override
public void focusLost(FocusEvent e) {
if (isEmpty) {
unregisterListeners();
try {
textfield.setText(ghostText);
textfield.setForeground(ghostColor);
} finally {
registerListeners();
}
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
updateState();
}
@Override
public void changedUpdate(DocumentEvent e) {
updateState();
}
@Override
public void insertUpdate(DocumentEvent e) {
updateState();
}
@Override
public void removeUpdate(DocumentEvent e) {
updateState();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
init();
}
});
}
public static void init() {
JFrame frame = new JFrame("Test ghost text");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JTextField textField = new JTextField();
JButton button = new JButton("Grab focus");
GhostText ghostText = new GhostText(textField, "Please enter some text here...");
textField.setPreferredSize(new Dimension(300, 24));
panel.add(textField);
panel.add(button);
frame.add(panel);
frame.pack();
frame.setVisible(true);
button.grabFocus();
}
}
* "No estoy buscando votos" * Difícil. +1 Chúpalo. ;) –
Esto puede ser un poco quisquilloso, pero ¿podría lograr esto sin eliminar el texto fantasma hasta que se escriba un carácter? Firefox puede mostrar de lo que estoy hablando: si abres una pestaña nueva, la barra de direcciones tiene un texto fantasma que dice "Buscar o ingresar dirección" que no desaparecerá cuando enfocas el campo, sino que se mantendrá hasta que ingreses el primer personaje. – ryvantage
Muchas gracias Guillaume, ¡esto es muy bueno!
acabo de cambiar algunas cosas para facilitar su uso:
- utilizado JTextComponent en lugar de JTextField por lo que funciona con todo el texto entradas
- llevó a cabo la clase de prueba y lo hizo público y no estático para que sea autónomo
Aquí está el código:
import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.JTextComponent;
public class GhostText implements FocusListener, DocumentListener, PropertyChangeListener
{
private final JTextComponent textComp;
private boolean isEmpty;
private Color ghostColor;
private Color foregroundColor;
private final String ghostText;
public GhostText(final JTextComponent textComp, String ghostText)
{
super();
this.textComp = textComp;
this.ghostText = ghostText;
this.ghostColor = Color.LIGHT_GRAY;
textComp.addFocusListener(this);
registerListeners();
updateState();
if (!this.textComp.hasFocus())
{
focusLost(null);
}
}
public void delete()
{
unregisterListeners();
textComp.removeFocusListener(this);
}
private void registerListeners()
{
textComp.getDocument().addDocumentListener(this);
textComp.addPropertyChangeListener("foreground", this);
}
private void unregisterListeners()
{
textComp.getDocument().removeDocumentListener(this);
textComp.removePropertyChangeListener("foreground", this);
}
public Color getGhostColor()
{
return ghostColor;
}
public void setGhostColor(Color ghostColor)
{
this.ghostColor = ghostColor;
}
private void updateState()
{
isEmpty = textComp.getText().length() == 0;
foregroundColor = textComp.getForeground();
}
@Override
public void focusGained(FocusEvent e)
{
if (isEmpty)
{
unregisterListeners();
try
{
textComp.setText("");
textComp.setForeground(foregroundColor);
}
finally
{
registerListeners();
}
}
}
@Override
public void focusLost(FocusEvent e)
{
if (isEmpty)
{
unregisterListeners();
try
{
textComp.setText(ghostText);
textComp.setForeground(ghostColor);
}
finally
{
registerListeners();
}
}
}
@Override
public void propertyChange(PropertyChangeEvent evt)
{
updateState();
}
@Override
public void changedUpdate(DocumentEvent e)
{
updateState();
}
@Override
public void insertUpdate(DocumentEvent e)
{
updateState();
}
@Override
public void removeUpdate(DocumentEvent e)
{
updateState();
}
}
cómo comprobar vacío jTextField programmically? Método getText return GhostText: | – do01
Bueno, y es aún mejor si, en el focoLost, agrega esto: else { unregisterListeners(); try { textComp.setForeground (foregroundColor); } finalmente { registerListeners(); } } –
- 1. texto fantasma - cómo tener en texto claro cuadro de texto
- 2. JTextField: Cómo configurar el texto a la izquierda de JTextField cuando el texto es demasiado largo
- 3. Texto de ejemplo en JTextField
- 4. OpenCV muestra la ventana gris
- 5. ¿Cómo validar un JTextField?
- 6. Cómo borrar JTextField cuando el mouse hace clic en JTextField
- 7. Cómo desenfocar un JTextField
- 8. Firebug muestra algunas solicitudes en gris
- 9. ¿Puedo limitar la longitud del texto en un JTextField que se puede pintar, mientras se almacena el texto completo?
- 10. ¿Alguien puede explicar la diferencia entre las referencias Fuerte, Suave, Débil y Fantasma y el uso que se le da?
- 11. El texto de UILabel no se muestra, pero el valor de "texto" se está actualizando (se muestra en la consola)
- 12. El texto de ListView está truncado: ¿cómo se muestra el texto completo de un elemento?
- 13. ¿Cómo hacer un formulario HTML en gris?
- 14. JTextArea y relleno interno de JTextField en el texto
- 15. cómo quitar MouseListener/ActionListener en un JTextField
- 16. Java, Swing: ¿cómo configuro el ancho máximo de un JTextField?
- 17. Copiar texto de una deshabilitada JTextField
- 18. Cómo recortar texto cuando se muestra un resumen del artículo?
- 19. ¿Hay alguna manera de evitar que el texto se vuelva gris cuando desactivo un botón?
- 20. establecer texto gris auxiliar para UISearchBar
- 21. Objeto débil en un NSDictionary?
- 22. Cómo convertir JTextField a String y String en JTextField?
- 23. ¿Por qué el texto se muestra más grande en xvfb?
- 24. Cómo limpiar el JTextField haciendo clic JButton
- 25. Deseleccionar selección predeterminada en JTextfield
- 26. Swing JTextField cómo quitar el borde?
- 27. No se puede eliminar el cambio fantasma pendiente de TFS
- 28. No puedo deshabilitar un punto de interrupción (fantasma)
- 29. Encontrar la posición del texto del cursor en JTextField
- 30. Xcode: ¿Cómo se muestra el valor de resistencia del GPS?
AF AIK, no. Pero me alegraría que me dijeran lo contrario –
Puedo decirle que Swing no proporciona esto de forma nativa (pero alguien puede haber escrito una biblioteca de terceros para hacerlo). –
Esto podría ayudar: http://stackoverflow.com/questions/1738966/java-jtextfield-with-input-hint – Ranga