Java - Problemas al saber si esta vacio o no un jtextfield

 
Vista:

Problemas al saber si esta vacio o no un jtextfield

Publicado por Edgar (1 intervención) el 13/07/2011 04:45:29
Hola a todos pues haciendo una practica en programacion, me he encontrado con un problema grande, tengo un JTextField y le agregue un Action released que va colocando automaticamente numeros, el problema es cuando borro con la tecla de borrar los numeros siguen apareciendo. he puesto

if(txtnumer.getText().equals(""){
System.out.println("Vacio");
}

pero esa validacion no me sirve ya que aunque no muestra nada de resultado, no lo toma como si estuviera vacio. ademas probe poniendo
if(txtnumer.getText().isEmpty())
System.out.println("Vacio");
}

tampoco me sirve me siguen apareciendo como si hubiera escrito algo D: no se que hacer gracias :)
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Problemas al saber si esta vacio o no un jtextfield

Publicado por Pedro (3 intervenciones) el 13/07/2011 20:26:28
... borro con la tecla de borrar

Que hace esa tecla cuando es presionada, como está definido el ActionListener para esa tecla?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Problemas al saber si esta vacio o no un jtextfield

Publicado por aitor (85 intervenciones) el 24/08/2011 10:32:37
mira este programa

/*
* NewJFrame.java
*
* Created on 24-ago-2011, 10:14:35
*/
package otro;

/**
*
* @author Aitor
*/
public class NewJFrame extends javax.swing.JFrame {

/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

texto = new javax.swing.JTextField();
boton = new javax.swing.JButton();
etiqueta = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

texto.setText("jTextField1");
texto.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
TextoKeyPresset(evt);
}
});

boton.setText("Borrar");
boton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
botonMousePressed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(101, 101, 101)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(etiqueta, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(texto, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(boton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(234, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(72, 72, 72)
.addComponent(texto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(boton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(etiqueta, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(141, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void botonMousePressed(java.awt.event.MouseEvent evt) {
etiqueta.setText("vacio");
texto.setText("");
}

private void TextoKeyPresset(java.awt.event.KeyEvent evt) {
if (etiqueta.getText().equals("vacio")) {
etiqueta.setText("");
}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton boton;
private javax.swing.JLabel etiqueta;
private javax.swing.JTextField texto;
// End of variables declaration
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar