Ver Mensaje Individual
  #3 (permalink)  
Antiguo 07/06/2013, 10:57
jurassicboy
 
Fecha de Ingreso: agosto-2009
Mensajes: 91
Antigüedad: 14 años, 8 meses
Puntos: 2
Respuesta: Comparar JTextField con cadena mientras se escribe.

Muchas gracias chuidiang, en efecto ha sido mucho más fácil con DocumentListener. Adjunto el código por si alguien tiene curiosidad por ver la resolución más adelante.

Código Java:
Ver original
  1. import javax.swing.*;
  2. import javax.swing.event.*;
  3.  
  4. public class Formulario extends JFrame implements DocumentListener{
  5.     private JLabel label1,label2,label3,label4;
  6.     private JTextField textfield1,textfield2;
  7.    
  8.     public Formulario() {
  9.         setLayout(null);
  10.         label1=new JLabel("Nombre:");
  11.         label1.setBounds(10,10,100,30);
  12.         add(label1);
  13.         label2=new JLabel("Clave:");
  14.         label2.setBounds(10,50,100,30);
  15.         add(label2);
  16.        
  17.         label3=new JLabel("");
  18.         label3.setBounds(250,10,100,30);
  19.         add(label3);
  20.        
  21.         label4=new JLabel("");
  22.         label4.setBounds(250,50,100,30);
  23.         add(label4);
  24.                
  25.         textfield1=new JTextField();
  26.         textfield1.setBounds(130,10,100,30);
  27.         add(textfield1);
  28.         textfield1.getDocument().addDocumentListener(this);
  29.         textfield2=new JTextField();
  30.         textfield2.setBounds(130,50,100,30);
  31.         add(textfield2);
  32.         textfield2.getDocument().addDocumentListener(this);
  33.                  
  34.                    
  35.     }
  36.  
  37.    
  38.     public void insertUpdate(DocumentEvent e) {  
  39.        
  40.        
  41.         if (textfield1.getText().equals(null)==false && textfield1.getText().isEmpty()==false){
  42.             if (textfield1.getText().equals("juan")) {
  43.                 label3.setText("Correcto");          
  44.            
  45.             }else{
  46.                
  47.                 label3.setText("Incorrecto");          
  48.                
  49.        
  50.             }
  51.            
  52.  
  53.            
  54.            
  55.            
  56.         }
  57.            
  58.             if  (textfield2.getText().equals(null)==false && textfield2.getText().isEmpty()==false){
  59.            
  60.             if (textfield2.getText().equals("abc123")) {
  61.                 label4.setText("Correcto");            
  62.                
  63.             } else {                   
  64.                
  65.                 label4.setText("Incorrecto");          
  66.                
  67.        
  68.             }
  69.          }
  70.            
  71.     }
  72.    
  73.    
  74.    
  75.     public void changedUpdate(DocumentEvent e) {
  76.    
  77.        
  78.     }
  79.  
  80.  
  81.    
  82.     public void removeUpdate(DocumentEvent e) {
  83.    
  84.         if (textfield1.getText().equals("")){
  85.             label3.setText("");
  86.             }
  87.        
  88.         if (textfield2.getText().equals("")){
  89.             label4.setText("");
  90.             }
  91.        
  92.         insertUpdate(e);
  93.                        
  94.        
  95.    
  96.        
  97.     }  
  98.  
  99.  
  100.    
  101.     public static void main(String[] ar) {
  102.         Formulario formulario1=new Formulario();
  103.         formulario1.setBounds(0,0,400,200);
  104.         formulario1.setVisible(true);
  105.     }
  106.  
  107.  
  108.      
  109. }