martes, 23 de enero de 2018

Interface FocuListener

La interfaz del oyente para recibir eventos de enfoque del teclado en un componente. La clase que está interesada en procesar un evento de enfoque implementa esta interfaz (y todos los métodos que contiene) o amplía la clase abstracta FocusAdapter (anulando solo los métodos de interés). El objeto detector creado a partir de esa clase se registra luego con un componente utilizando el método addFocusListener del componente. Cuando el componente gana o pierde el foco del teclado, se invoca el método relevante en el objeto oyente y se le pasa el FocusEvent

Permite que el oyente este escuchando en cada momento al hacer un click en cualquier para del JPanel, JFrame, JButton, etc esto comprende dos metodos focusGained(), o focusLost()


focusGained(focusEvent e), cuando el componente ha ganado el keyboard focus

focusLost(focusEvent e), cuando el componente ha perdido el keyBoard focus

PARA MAS INFORMACION VISITAR LA PAGINA DE LA API DE JAVA 

EJEMPLO

package graficos;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.HeadlessException;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.geom.Rectangle2D;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author USUARIO
 */
public class Event_Foco {
   
    public static void main(String[] args) {
        principal prin = new principal();
        prin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
    }
}

class principal extends JFrame {
   
    public principal() {
       
        setBounds(300, 200, 500, 500);
        setVisible(true);
        add(new focoBotonoes());
       
    }
   
    private class focoBotonoes extends JPanel {
       
        JButton boton1;
        JButton boton2;
        JTextField texto1;
        JTextField texto2;
        JLabel respuesta;
       
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            setLayout(null);
            dibujarBotones();
            Rectangle2D rectangulo = new Rectangle2D.Double(120, 120, 192, 374);
            g2.draw(rectangulo);
            texto1.addFocusListener(new lanzarFocus());
            texto2.addFocusListener(new lanzarFocus());
        }
       
        void dibujarBotones() {
            boton1 = new JButton("buton 1");
            texto1 = new JTextField();
            boton1.setBounds(40, 90, 80, 40);
            texto1.setBounds(136, 90, 96, 40);
           
            boton2 = new JButton("boton 2");
            texto2 = new JTextField();
            boton2.setBounds(40,150,80,40);
            texto2.setBounds(136,150,96,40);
           
            add(boton1);
            add(boton2);
            add(texto1);
            add(texto2);
           
        }
       
       private class lanzarFocus implements FocusListener {
           
            public void focusGained(FocusEvent fe) {
             
                if (fe.getSource() == texto1) {
                    System.out.println("El texto 1 ha ganado el foco");
                } else if (fe.getSource() == texto2) {
                    System.out.println("El texton 2 ha ganadado el foco");
                }
            }
           
            @Override
            public void focusLost(FocusEvent fe) {
             
                if (fe.getSource() == texto1) {
                    System.out.println("El texto 1 ha perdido el foco");
                    if (!revisisarContenido(texto1.getText())) {
                        crearLabelRevisador(texto1.getX(), texto1.getY(), "falta arroba");
                    }
                } else if (fe.getSource() == texto2) {
                    System.out.println("El texto 2 ha perdido el foco");
                    if (!revisisarContenido(texto2.getText())) {
                        crearLabelRevisador(texto2.getX(), texto2.getY(), "falta arroba");
                    }
                }
            }
           
            boolean revisisarContenido(String texto) {
                char[] separad = texto.toCharArray();
                boolean comprobador = false;
                for (char letra : separad) {
                    if (letra == '@') {
                        comprobador = true;
                    }
                }
                return comprobador;
               
            }
           
            void crearLabelRevisador(int x, int y, String respues) {
                respuesta = new JLabel();
                respuesta.setBounds(y, y + 20, 80, 40);
                respuesta.setText(respues);
                add(respuesta);
            }
        }
       
    }
}


Share:

0 comentarios:

Publicar un comentario

BTemplates.com

Buscar este blog

Archivo del Blog

Con tecnología de Blogger.