Capture the text of JTextField - it does not work

0

I am learning this wonderful and complex language. Following a video course (from youtube) in which is the following exercise:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package graficos;

import java.awt.Graphics;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Elio Gabriel Drovandini
 * @version 1.0
 */
public class FocoEvento {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        MarcoFoco miMarco = new MarcoFoco();
        miMarco.setVisible(true);
        miMarco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

class MarcoFoco extends JFrame{

    public MarcoFoco(){
        setTitle("Eventos de foco en componentes");
        setLocation(600, 250);
        setSize(750, 350);
        PanelFoco miLamina = new PanelFoco();
        add(miLamina);
    }

}

class PanelFoco extends JPanel{

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        setLayout(null);
        //Al invalidar el Layout se debe definir posiscion y tamaño para loc compoenntes
        cuadroTexto1 = new JTextField();
        cuadroTexto1.setBounds(120, 10, 150, 20);
        add(cuadroTexto1);
        cuadroTexto2 = new JTextField();
        cuadroTexto2.setBounds(120, 50, 150, 20);   
        add(cuadroTexto2);
        EventoFoco focoComponente = new EventoFoco();
        cuadroTexto1.addFocusListener(focoComponente);
    }

    private class EventoFoco implements FocusListener{

        @Override
        public void focusGained(FocusEvent fe) {
            System.out.println("Se ha ganado el foco del cuadro de texto 1");
        }

        @Override
        public void focusLost(FocusEvent fe) {
            String correo = cuadroTexto1.getText();
            boolean check = false;
            for(int i = 0; i < correo.length(); i++){
                if(correo.charAt(i) == '@'){
                    check = true;
                }
            }
            if(check){
                System.out.println("La dirección es válida");
            } else{
                System.out.println("La dirección es inválida");
            }
        }

    }

    JTextField cuadroTexto1;
    JTextField cuadroTexto2;
}

I am faithfully reproducing the proposed exercise. It is about the focus events of components. The problem is punctually given in the line:

String correo = cuadroTexto1.getText();

inside the focusLost method. because it does not work (it seems to do nothing) if the video is made in the same way and it works. To verify this I have tried several variants of capturing the text of one frame and the other in different points, but I have been able to verify that this line is apparently the one that is apparently being ignored. As a final clarification I use Netbeans 8.1 with SDK 1.8 and in the course Eclipse with SDK 1.7 is used. Thank you very much for your attention. Best regards and I await your response .-

    
asked by gabrieldrv 22.09.2017 в 01:43
source

1 answer

0

I do not understand your problem very well but I can tell you that if you are capturing the data, just that you are not printing it, try this code, replacing the focusLost method

@Override

    public void focusLost(FocusEvent fe) {
        String correo = cuadroTexto1.getText();
        boolean check = false;
        for(int i = 0; i < correo.length(); i++){
            if(correo.charAt(i) == '@'){
                check = true;
            }
        }
        System.out.println("primer cuadro"+cuadroTexto1.getText());
         System.out.println("segundo cuadro"+cuadroTexto2.getText());

        if(check){
            System.out.println("La dirección es válida");
        } else{
            System.out.println("La dirección es inválida");
        }
    }
    
answered by 22.09.2017 в 16:23