VISITAR LA API DE ORACLE PARA MAS INFORMACION
EJEMPLO DE LA UTILIZACION DE FOCUS LISTENER
/*
* 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 principal;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Principal extends JFrame{
JTextSombra txtnombre,txttel,txtcorreo;
public Principal() {
JPanel pdatos=new JPanel();
pdatos.setLayout(new GridLayout(3,2));
pdatos.add(new JLabel("Nombre: "));
txtnombre=new JTextSombra("Escriba su aqui su nombre");
pdatos.add(txtnombre);
pdatos.add(new JLabel("Telefono: "));
txttel=new JTextSombra(20);
pdatos.add(txttel);
pdatos.add(new JLabel("E-maiil: "));
txtcorreo=new JTextSombra("Correo Electronico",20);
pdatos.add(txtcorreo);
JButton btnmostrar=new JButton("Mostrar");
btnmostrar.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "Nombre: "+txtnombre.getText()+", Telefono: "+txttel.getText()+", Correo Electronico: "+txtcorreo.getText());
}
});
add(pdatos);
JPanel pboton=new JPanel();
pboton.add(btnmostrar);
add(pboton,BorderLayout.SOUTH);
}
public static void main(String arg[]){
Principal p=new Principal();
p.setVisible(true);
p.setBounds(0, 0, 400, 180);
p.setLocationRelativeTo(null);
p.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
//CLASE DONDE SE EJECUTA CUANDO SE HACE CLICK EN EL JTEXTFILD
/*
* 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 principal;
import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JTextField;
public class JTextSombra extends JTextField implements FocusListener {
String texto = "Escriba aqui...";
public JTextSombra() {
this.setText(texto);
this.setForeground(Color.RED);
this.addFocusListener(this);
}
public JTextSombra(int lg) {
super(lg);
this.addFocusListener(this);
}
public JTextSombra(String texto) {
this.texto = texto;
this.setText(texto);
this.setForeground(Color.RED);
this.addFocusListener(this);
}
public JTextSombra(String texto, int lg) {
super(lg);
this.texto = texto;
this.setText(texto);
this.setForeground(Color.RED);
this.addFocusListener(this);
}
public void focusLost(FocusEvent arg0) {
if (JTextSombra.this.getText().equalsIgnoreCase("")) {
JTextSombra.this.setText(texto);
JTextSombra.this.setForeground(Color.RED);
}
}
@Override
public void focusGained(FocusEvent fe) {
if (JTextSombra.this.getText().equalsIgnoreCase(texto)) {
JTextSombra.this.setText("");
JTextSombra.this.setForeground(Color.black);
}
}
}
WindowFocusListener
La interfaz del oyente para recibir WindowEvents, incluidos los eventos WINDOW_GAINED_FOCUS y WINDOW_LOST_FOCUS. La clase que está interesada en procesar un evento de ventana implementa esta interfaz (y todos los métodos que contiene) o amplía la clase abstracta WindowAdapter (anulando solo los métodos de interés).
para saber que ventana ha ganado y perdido el foco
Esta interfas cuenta con dos metodos:
public void windowGainedFocus(WindowEvent we){
}La cual nos hace referencia a que ventana a ganado el focus
public void windowLostFocus(WindowEvent we)(
)
Nos hace referencia a que ventana ha perdido el focus
PARA MAYOR INFORMACION VISITAR LA PAGINA DE LA API DE JAVA
EJEMPLO
/*
* 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.event.FocusAdapter;
import java.awt.event.FocusListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import javax.swing.JFrame;
/**
*
* @author USUARIO
*/
public class Focus_enVentanas extends JFrame implements WindowFocusListener {
Focus_enVentanas ventana1;
Focus_enVentanas ventana2;
public static void main(String[] args) {
Focus_enVentanas ventana = new Focus_enVentanas();
ventana.inciar();
}
public void inciar() {
ventana1 = new Focus_enVentanas();
ventana1.setVisible(true);
ventana1.setBounds(15, 20, 400, 200);
ventana2 = new Focus_enVentanas();
ventana2.setVisible(true);
ventana2.setBounds(500, 60, 400, 200);
ventana1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana1.addWindowFocusListener(this);
ventana2.addWindowFocusListener(this);
}
@Override
public void windowGainedFocus(WindowEvent we) {
Object ventan = we.getSource();
if (ventan == ventana1) {
System.out.println("el foco esta en la venta 1");
} else if (ventan == ventana2) {
System.out.println("el foco esta en la venta 2");
}
}
@Override
public void windowLostFocus(WindowEvent we) {
Object ventan = we.getSource();
if (ventan == ventana1) {
System.out.println("ha perdido la venta 1");
} else if (ventan == ventana2) {
System.out.println("ha perdido la venta 2");
}
}
}






0 comentarios:
Publicar un comentario